We already learned about variables, but there are some Readymade or predefined keywords in PHP that can be used without you having to create them first. Predefined or Superglobals variables are used to provide information from and about the web server, the web browser, and the user. There are several types of Super Global Variables:- $_GET – Is used to pass data through URL. Example: http://websjyoti.com?id=2 $_POST – Is used to pass data to server by hiding from URL. $_REQUEST – Is the combination of $_GET, $_POST, and $_COOKIE. $_SESSION – is used to maintain session and data from Local machine to Server. $_COOKIE – Is used to save/get user data on same machine in browser cookies. $_SERVER – Is an associative array which is used to know server information i.e. IP address, Host name, query string, file name etc. $_FILES – Is used to handle uploaded files operations in PHP. i.e. file size, type, name etc. Example – $_SERVER Superglobals: <?php echo”Current IP Address is: “.$_SERVER[‘REMOTE_ADDR’]; echo “You are at page: “.$_SERVER[‘PHP_SELF’]; ?> Functions: Functions in PHP behave similarly to functions in C. When we define the functions, we must specify what values the function can expect to receive. So let’s create a function. We need to give the function a name and tell it what variables to expect. We also need to define the function before we call it. <?php function sum($f1, $f2) { $f3=$f1+$f2 return $f3; } echo sum(10,8); ?> Output: 18 So First, we created our function. Notice how we defined two new variables, called $f1 and $f2. When we call the function, each variable is assigned a value based on the order in which it appears. Then we simply added the two numbers together and returned the result. “Return” here simply means to send the result back.
Arrays in PHP
An array is a data structure that stores similar type of values in a common variable. Arrays are also called homogeneous data type. For example if you want to store 50 numbers then instead of defining variables it is easy to define an array with size of 50. In PHP, the array() function is used to create an array Arrays can store numbers, strings and any object but their index will be represented by numbers. By default array index starts from zero. $x=array(“laxmi”,”hirdesh”,”rahim”,”ashu”,”amit”); // To count number of elements stored in array x echo count($x); // To populate value from any specific index or position //echo $x[1]; // output – Hirdesh // To print all elements in array x – using for-each loop echo ” <br>Names are :”; foreach ($x as $val) { echo $val . ” “; } echo “<br> using for loop <br>”; 50 for($i=0; $i<count($x); $i++) { echo $x[$i] . “<br>”; } Sorting Arrays: PHP provides many easy ways to sort array values in predefined orders- arsort: Sorts the array in descending value order and maintains the key/value relationship asort: Sorts the array in ascending value order and maintains the key/value relationship rsort: Sorts the array in descending value order sort: Sorts the array in ascending value order // Examples – Sorting of arrays $x=array(“laxmi”,”hirdesh”,”rahim”,”ashu”,”amit”, “mohit”); // rsort for DESC and simply sort for ASC sort($x); foreach ($x as $val) { echo $val . ” “; }
PHP – Date / Time Functions
When working in any programming language, dealing with dates and time is a simple task. That is, until time zones have to be supported. PHP’s time () function gives you all the information that you need about the current date and time. It requires no arguments but returns an numeric value. If you will call to time() function which will return a set of numeric values which is difficult to understand. So to make it easy we can have time format along with the date() function. <?php $today = date(“d/m/Y”); echo “Today is “.$today; ?> We can use different time format with separator like d-m-Y, d.m.Y and so on. Display Current Date and Time: We can use the following characters to format the time string: <?php echo date(“h:i:s”) . “<br>”; echo date(“F d, Y h:i:s A”) . “<br>”; echo date(“h:i a”); ?> Here h-hour in 12-hour format, H – hour in in 24-hour format, i – minutes, s – seconds, a -am, pm(Lowercase) and A – Represent AM, PM (Uppercase) So it is easy to get the current year by putting the following line- <?php echo date(“Y”);?>?> The PHP time(): The time() function is used to get the current time as a Unix timestamp Example: <?php $cDate = 1394003958; echo(date(“F d, Y h:i:s”, $cDate)); ?> Example: To print date like 10 July 2015 <?php $date1 = date_create(―2016-05-10”); echo date_format($date1, ‘j-F-Y’); ?> Output: 10 May 2016