Вы находитесь на странице: 1из 11

Write simple program to display the current date To develop the first program of PHP, you need to have

a web server like: Apache Tomcat , IIS etc. Another way to develop PHP program. WAMP is a kind of stack of software collection, WAMP is the acronym for Windows, Apache, MySQL, and PHP. WAMP server is one of the popular server use for PHP. It is free and you can download it from http://www.wampserver.com/en/download.php (you can copy and paste this link in the browsers address bar). After downloading the .exe file, install this file (normal installation as we do for other software package), For downloading and installing please visit our web site:http://roseindia.net/tutorial/php/phpbasics/Downloading-Installing-Wamp.html For starting the WAMP server: http://roseindia.net/tutorial/php/phpbasics/Running-Testing-Wamp.html Now to develop the first program do as follows: Exmaple 1: <?php echo date("l"); ?> Output: Tuesday In PHP date() is a predefined function which returns a string value, depends on the parameter passed. Array in PHP Almost every language supports array, which is a collection of similar data type, but in PHP language it could be or could not be the same. An array could be a collection of similar or dissimilar datatype. PHP Array Example 1: <?php

echo"An array with similar datatype"."<br/>"; $array=array("These","are","similar"); var_dump($array); echo"An array with dissimilar datatype"."<br/>"; $array=array("String",122,34.34,true,'v'); var_dump($array); ?> Output: An array with similar datatype array(3) { [0]=> string(5) "These" [1]=> string(3) "are" [2]=> string(7) "similar" } An array with dissimilar datatype array(5) { [0]=> string(6) "String" [1]=> int(122) [2]=> float(34.34) [3]=> bool(true) [4]=>

string(1) "v" } Array in PHP with Example 2: <?php echo"An array with similar datatype"."<br/>"; $array=array("These","are","similar"); var_dump($array); echo"var_dump() is a function which is used to print the elements of an array"; echo "<br/>Another function is print_r(), which is used to print an array<br/>"; print_r($array); ?> Output: An array with similar datatype array(3) { [0]=> string(5) "These" [1]=> string(3) "are" [2]=> string(7) "similar" } var_dump() is a function which is used to print the elements of an array Another function is print_r(), which is used to print an array Array ( [0] => These [1] => are

[2] => similar ) Example 3: <?php echo"This is an example of numeric array<br/>"; $array=array("This","is","numeric","array"); var_dump($array); echo"This is an example of associative array<br/>"; $array=array(1=>"This","s"=>2,3=>23.23); var_dump($array); ?> Output: This is an example of numeric array array(4) { [0]=> string(4) "This" [1]=> string(2) "is" [2]=> string(7) "numeric" [3]=> string(5) "array" } This is an example of associative array array(3) { [1]=> string(4) "This"

["s"]=> int(2) [3]=> float(23.23) }

Example 4: <?php echo "This is an example of numeric array<br/>"; $array=array( "string"=>array( "This", "is", "multidimensional", "array"), "numeric"=>array(1,2,2,223,34)); var_dump ($array); ?> Output: This is an example of numeric array array(2) { ["string"]=> array(4) { [0]=> string(4) "This" [1]=> string(2) "is" [2]=> string(14) "multidimensional" [3]=> string(5) "array" } ["numeric"]=>

array(5) { [0]=> int(1) [1]=> int(2) [2]=> int(2) [3]=> int(223) [4]=> int(34) } }

Mathematical Operations in PHP PHP supports mathematical operations as other languages do. It support addition, subtraction, division, multiplication, increment, decrement etc. Example 1: <?php $a=25; $b=5; $c=$a+$b; echo "<br/> Addition of $a and $b is=$c"; $c=$a-$b; echo "<br/> Subtraction of $a and $b is=$c"; $c=$a*$b;

echo "<br/> Multiplication of $a and $b is=$c"; $c=$a/$b; echo "<br/> Division of $a and $b is=$c"; echo "<br/> Before Increment value of \$a is=$a"; $a++; echo "<br/> After Increment value of \$a is=$a"; echo "<br/> Before Decrement value of \$b is=$b"; $b--; echo "<br/> After Decrement value of \$b is=$b"; ?> Output: Addition of 25 and 5 is=30 Subtraction of 25 and 5 is=20 Multiplication of 25 and 5 is=125 Division of 25 and 5 is=5 Before Increment value of $a is=25 After Increment value of $a is=26 Before Decrement value of $b is=5 After Decrement value of $b is=4

PHP User Defined Function An user-defined function saves us from rewriting the same code again and again and helps us to make our application smaller. The given examples will help to understand how you can put them to work.

PHP has so many built-in functions, besides that you can make your own functions as per the need. General structure of the user defined function is: function function_name([mixed $var,.....]){ PHP code} mixed is the datatype that means it could be of any datatype. $var: is the name of the parameter, one function could have one, more than one or no parameter. Example 1: <?php function add($first,$second) { return ($first+$second); } $first=12; $second=13; echo "function add will return=".add($first,$second); /*You might know these variables are * different from the parameters of add function. */ ?> Output: function add will return=25 Example 2:

<?php function add($first,$second=23) { return ($first+$second); } $first=100; echo "function add will return=".add($first); ?> Output: function add will return=123 Example 3: <?php function one() { echo "We are inside the function."; } echo "We are outside the function<br/> "; one(); ?> Output:

We are outside the function We are inside the function. Example 4: <?php function string_push($array,$str) { return(array_push($array,$str)); } $array=array("This","is","new"); echo"<br/><b>Before calling string_push function</b><br/>"; var_dump($array); echo"<br/><b>After calling string_push function</b><br/>"; $array=array($array,"array"); var_dump($array); ?> Output: Before calling string_push function array(3) { [0]=> string(4) "This" [1]=> string(2) "is" [2]=> string(3) "new"

After calling string_push function array(2) { [0]=> array(3) { [0]=> string(4) "This" [1]=> string(2) "is" [2]=> string(3) "new" } [1]=> string(5) "array" }

Вам также может понравиться