Showing posts with label PHP. Show all posts
Showing posts with label PHP. Show all posts

Thursday, 4 December 2014

How to find execution time for a script in PHP

if your are a PHP dev and you execute php scripts to test time taken, the following code can help you


microtime — Return current Unix timestamp with microseconds



[php]
//Start time
$time_start = microtime(true);

// Sleep for 1 sec
sleep(1);

//Execution of your script

//End time
$time_end = microtime(true);

//Time taken
$time = $time_end - $time_start;

echo "Time taken is $time seconds\n";

[/php]

Hope this will be useful in your application
Ref

Wednesday, 31 July 2013

Difference between PHP4 and PHP5



Advantages of PHP5

Static Methods and Variables

Visitbility
Public , Private and Protected

Abstract Classes

Interfaces

Magic Methods

Final Keyword

__autoload Function

Type Hinting

Exceptions



Ref: http://www.webmaster-talk.com/php-forum/78717-differences-between-php4-and-php5.html

Sunday, 13 January 2013

How to replace a URL to an Hyperlink in a String?

At times you come across a scenario where the string contains an http link and you need to create the hyperlink to the same.

This is can very well achieved using the single line of code.





$text = "This is http://www.phpcoaching.in site";
echo $text = ereg_replace("[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]", "\\0", $text);

?>



Source

How to repeat a string n times in PHP?

Question
How to repeat a string n times in PHP?

Answer


echo str_repeat(" www.phpblog.in ", 3);
?>


Explanation

This function will repeat a particular string 3times and display on the screen

How to get last element of an array?

Answer:
To get the last element use the end()



$arr = array('php', 'online', 'tutorials');
echo end($arr); // tutorials

?>

How to define a variable in PHP?

How to define a variable in PHP?



define("ABC","Online Training");
echo ABC;//Online Training

?>

Also if I change the value of the variable at later stages what will happen



define("ABC","Online Training");
echo ABC;//Online Training
............
............
............
define("ABC","Online Training Institute");
echo ABC;//Online Training

?>

Even if we change the value it will display the previously set value only

How to start session and what will happen if I start session twice?

How to start session in PHP



session_start();

?>

What will happen if I write session_start(); twice



session_start();
echo session_id();

echo "
";

session_start();
echo session_id();

?>


Reason : As per the above code writing it twice will not make any difference to the code and it will continue to keep the previous generated session_id();

How to add one day in PHP?

The following code is used many a times in our web development, we always try to add one day in today’s date to get tomorrow’s date. The following code will surely help you doing this.



$todayDate = date("Y-m-d");// current date

echo "Today: ".$todayDate."
";

//Add one day to today
$date = strtotime(date("Y-m-d", strtotime($todayDate)) . " +1 day");
echo "After adding one day: ".date('l dS \o\f F Y', $date)."
";

?>

How to create ICS/iCal file using PHP?

Many a times we come across a requirement of creating ICS/iCal file from PHP, First of all iCal file is a calendar event file which can be imported in many calendar such as Google calendar, MS Outlook, etc. Its a standard file format. This code snippet gives you idea to create iCal file from PHP which can be imported to Google or Outlook


//This is the most important coding.
header("Content-Type: text/Calendar");
header("Content-Disposition: inline; filename=filename.ics");
echo "BEGIN:VCALENDAR\n";
echo "PRODID:-//Microsoft Corporation//Outlook 12.0 MIMEDIR//EN\n";
echo "VERSION:2.0\n";
echo "METHOD:PUBLISH\n";
echo "X-MS-OLK-FORCEINSPECTOROPEN:TRUE\n";
echo "BEGIN:VEVENT\n";
echo "CLASS:PUBLIC\n";
echo "CREATED:20091109T101015Z\n";
echo "DESCRIPTION:Testing ICS Download\\n\\n\\nEvent Page\\n\\nhttp://www.mysite.com\n";
echo "DTEND:20091208T040000Z\n";
echo "DTSTAMP:20091109T093305Z\n";
echo "DTSTART:20091208T003000Z\n";
echo "LAST-MODIFIED:20091109T101015Z\n";
echo "LOCATION:Anywhere have internet\n";
echo "PRIORITY:5\n";
echo "SEQUENCE:0\n";
echo "SUMMARY;LANGUAGE=en-us:Testing Event\n";
echo "TRANSP:OPAQUE\n";
echo "UID:040000008200E00074C5B7101A82E008000000008062306C6261CA01000000000000000\n";
echo "X-MICROSOFT-CDO-BUSYSTATUS:BUSY\n";
echo "X-MICROSOFT-CDO-IMPORTANCE:1\n";
echo "X-MICROSOFT-DISALLOW-COUNTER:FALSE\n";
echo "X-MS-OLK-ALLOWEXTERNCHECK:TRUE\n";
echo "X-MS-OLK-AUTOFILLLOCATION:FALSE\n";
echo "X-MS-OLK-CONFTYPE:0\n";
//Here is to set the reminder for the event.
echo "BEGIN:VALARM\n";
echo "TRIGGER:-PT1440M\n";
echo "ACTION:DISPLAY\n";
echo "DESCRIPTION:Reminder\n";
echo "END:VALARM\n";
echo "END:VEVENT\n";
echo "END:VCALENDAR\n";
?>

Source

Where and How to use autoload function in PHP?

Many a times when we use OOP in PHP we define class files and include it in our code. But in PHP we can include class file on the go using autoload function.




function __autoload($class_name)
{
$file_path = "PATH_TO_CLASS_FILE";
if (file_exists($file_path))
{
require($file_path);
}
}


The above code can be written any where in our code, at run time when we wish to create a object of any class, it will include the class file on the go and function would be accessed and required operation can be performed.

To explain the above with example, consider you created a class file in suppose “classes” folder and named it as “login.class.php” and while executing the code you create the object of the login class as


$obj = new login;

While executing the above code, it will first include the login class file and object would be created.

The advantages of this is that we can use n no of class files within a page without checking or including all class files manually

Comments in PHP

Comments in PHP



//This is a single line comment

/*
This is
a comment
block
or
Multi line Comment
*/
?>

How to use variables in PHP?

Now you are very much familiar with displaying any sample text in PHP using syntax, today we would learn how php variables play an important roles in programming.

When we say “variables”, what instantly come to your mind?

Anything whose value keeps changing.

Variable in PHP are declared using “$”. I would elaborate it with example



$var_name = "value";
?>


The above syntax is explained as “$var_name” is the name of the variable and “value” will be the value which we wish to assign to this variable.

This would be better understood with following example:


$my_name = "www.phpblog.in";
echo $my_name;
?>

As you can see that we can assign any string to the variable and for displaying the value we can use the “echo” command

Print a string in PHP?

To start programming in PHP, lets start with running a small code to print a line:


echo "Hello World.!";
?>


The following code will display “Hello World.!” when the code is executed on browser

What is PHP and where is it used?

Welcome to world of open source technology.

PHP is an open source programming language.

PHP is acronym for Hypertext Preprocessor, Sometimes people do refer it as “Personal Home Page”

PHP when used with MYSQL can be used for creating dynamic websites.

PHP is one of the powerful tools is open source technologies.

People now-a-days prefer using LAMP(LINUX-APACHE-MYSQL-PHP) technologies for creating various websites and web application.

Popular websites and application built on PHP

As we go about learning PHP and its dependent languages, we must first understand and see which all websites are built on PHP.

As I have stated in my earlier post advantages of PHP and its vast usage, PHP has penetrated deep into the internet industry.

To name a few popular application and websites built using PHP:

  1. http://wordpress.com : WordPress is a blogging tool which is used widely by many of the bloggers to have their own blog hosted. Its one of the best blogging tool and some times used as CMS as well

  2. http://www.joomla.org/ : Joomla is one the oldest CMS tool which is developed on PHP

  3. http://www.magentocommerce.com/ : Magento is one of the popular Shopping Cart tool built on PHP. Magento is widely used for developing shopping cart application.