PHP
PHP
$num=1;
echo $num;
echo "<br>";
$name="hari";
echo $name;
echo "<br>";
$myName="harsh";
echo $myName;
?>
Expression in PHP
• The combination of operands with an operator to produce a result is
called an Expression.
• Operators are used to perform operations on variables or values.
• $num1 + $num2
• Here $num1 and $num2 are operands and + is an operator.
Arithmetic operator in PHP
• +, -, *, / are arithemetic operator.
• modulus operator (%) returns the division remainder.
Power of number in PHP
• <?php
• $num1=2;
• $num2=3;
• $ans=pow($num1,$num2);
• echo $ans;
• ?>
Area of shape in PHP
<?php #Area of square
$side=4;
#Area of circle $area=$side*$side;
$r=2; echo $area;
$area=3.14*$r*$r; echo "<br>";
echo $area;
echo "<br>"; #Area of rectangle
$l=6;
#Area of triangle $b=5;
$base=3; $area=$l*$b;
$height=2; echo $area;
$area=$base*$height/2; echo "<br>";
echo $area; ?>
echo "<br>";
Single Vs Double Quotes in PHP
• The simplest way to specify a string is to enclose it in single quotes.
• Single quote is generally faster, and everything quoted inside treated
as plain string.
• Doble quote allow you to use \n, \r and \t and variables.
• An important point here is that you can use curly braces to isolate the
name of variable you want evaluated.
• e.g.
$name= “hari”;
echo “My name is $name <br>”;
echo ‘My name is $name’;
Output : My name is hari.
My name is $name
• We can not print double quote inside double quote and single quote inside
single quote.
• But using forward slash(/) we can print double quote inside double quote.
<?php
?>
If else statement in PHP
• The if statement executes some code if one condition is true.
• The if...else statement executes some code if a condition is true and
another code if that condition is false.
<?php <?php
$day= “sunday”; $num=12;
if($day== “sunday”) if($num%2==0)
echo “Today is holiday.”; echo “Number is divisible by 2.”
else else
echo “Today is not holiday.”; echo “Number is not divisible by 2.”
?> ?>
If...else if...else statement
• The if...else if...else statement executes different codes for more than
two condition.
<?php
$num=17;
if($num%2==0)
echo “Number is multiple of 2.”;
else if($num%3==0)
echo “Number is multiple of 3.”;
else if($num%5==0)
echo “Number is multiple of 5.”;
else
echo “Number is not multiple of 2, 3, 5.”
?>
Concatination operator in PHP
• The PHP concatination operator (.) is used to combine two string
values to create one string.
• .= concatination assignment
<?php
$name= “hari”;
$degree= “B.Tech”;
$age = 19;
echo “My name is ”.$name.“ . I have ”.$degree.“ degree. I am ”.$age.“ years old.”;
?>
<?php
$name= “hari”;
$name.= “ pande ”; // $name=$name.“ pande “;
echo $name
?>
Comparison Operator in PHP
• Comparison operator, as their name implies, allow you to compare
two values.
• ==, !=, <, >, <=, >= are comparison operator.
<?php
$num1=5;
$num2=5;
if($num1 == $num2)
echo $num1 . ‘ is equal to ’.$num2;
else
echo $num1. ‘ is not equal to ‘.$num2;
?>
Logical operators in PHP
• or, and, xor, &&, || are logical operators.
<?php
$a=2;
$b=3;
$c=2;
?>
PHP Ternary Operator
• The Ternary operator takes three operands - a condition, a result for
true, and a result for false.
<?php
$a=5;
echo ($a==5) ? “$a is equal to 5.” : “$a is not equal to 5.”
?>
Increment And Decrement Operator
• PHP support C-style pre and post increment and decrement operator.
• ++$a : Pre-increment
• $a++ : Post-increment
• --$a : Pre-decrement
• $a-- : Post-decrement
PHP switch case statement
• The switch statement is used to perform different actions based on
different conditions.
<?php
Sa=1;
switch($a)
{
case 1:
echo “Number is 1.”;
break;
case 2:
echo “Number is 2.”;
break;
default:
echo “Number is not known.”;
break;
}
?>
Run PHP inside HTML Tag
Genrate random number in PHP
• PHP has inbuilt function rand().
<?php
$num=rand();
echo $num;
?>
Fizz Buzz program
<?php else
$heads=0; {
$tails=0; #echo "Tails,you win!";
for($i=1;$i<=100;$i++) $tails++;
{ }
$num=rand(1,2); }
if($num==1) echo "Heads : ".$heads."<br>". "Tails : ".$tails;
{ ?>
#echo "Heads,I win!";
$heads++;
}
Length of words or sentence in PHP
• strlen() function returns the length of string.
<?php
$name="hari";
$len=strlen($name);
echo $len;
?>
Count number of words in string in PHP
• str_word_count() function counts the number of words in a string.
<?php
$name="Mere pyare hari";
$count=str_word_count($name);
echo $count;
?>
Replace character/words in string in PHP
• str_replace() function replaces some characters with some other
characters in a string.
• This function is a case-sensitive. Use the str_ireplace() function to
perform a case-sensitive search.
• str_replace( search, replace, subject )
PHP functions
• Besides the built-in PHP functions, it is possible to create your own
functions.
• A function is a block of statements that can be used repeatedly in a
program.
• A function will not execute automatically when a page loads.
• A function will be executed by a call to the function.
• Function names follow the same rules as other labels in PHP.
• A valid function name starts with a letter or underscore, followed by
any number of letters, numbers, or underscores.
How to pass parameters in function in PHP
<?php <?php
function sun($a,$b) #function parameter function mul($a,$b)
{ {
$ans=$a+$b; return $a*$b;
echo $ans; }
} $a=5;
sum(15,5); #function arguments $b=4;
sum(25,5); echo “Multiplication of ”.$a. “ and ”.$b. “ = ”.mul($a,$b);
?> ?>
Default arguments in function in PHP
<?php
function mul($a,$b=5)
{
$ans=$a*$b;
echo $ans;
}
mul(4,2)
mul(5,4);
mul(2);
?>
PHP Arrays
• An array is a special variable, which can hold more than one value at a
time.
• Array can be used to store linear data of similar type.
• In PHP, the array() function is used to create an array:
• There are 3 types of arrays:
• Indexed arrays : Arrays with a numeric index
• Associative arrays : Arrays with named keys
• Multidimensional arrays - Arrays containing one or more arrays
Inbuilt function of counting length
• count($name_of_array) function returns length of array.
<?php
$name=array("Hari","Nilkanth","Sahjanand","Swaminarayan");
echo "<ol>";
for($i=0;$i<count($name);$i++)
{
echo "<li>".$name[$i]. "</li>";
}
echo "</ol>"
?>
PHP foreach loop
• The foreach loop - loops through a block of code for each element in
an array.
• The foreach loop works only on arrays, and is used to loop through
each key/value pair in array.
<?php
$name=array("Hari","Nilkanth","Swaminarayan","Sahjanand");
echo "<ol>";
foreach($name as $i)
{
echo "<li>".$i."</li>";
}
echo "</ol>";
?>
Inbuilt function for sort and reverse
<?php
$num=array(59,55,2,67);
echo "Before sort Array : <br>";
foreach($num as $i)
{
echo $i. " ";
}
sort($num);
echo "<br>After sort Array : <br>";
foreach($num as $i)
{
echo $i. " ";
}
rsort($num); #for reverse sorting
echo "<br>After reverse sort Array : <br>";
foreach($num as $i)
{
echo $i. " ";
}
?>
<?php rsort($name);
$name=array("Hari","Nilkanth","Swaminarayan","Sahjanand"); echo "<br>After reverse sorting : <br>";
echo "<ol>"; echo "<ol>";
foreach($name as $i) foreach($name as $i)
{ {
echo "<li>".$i."</li>"; echo "<li>".$i."</li>";
} }
echo "</ol>"; echo "</ol>";
?>
sort($name);
echo "<br>After sorting : <br>";
echo "<ol>";
foreach($name as $i)
{
echo "<li>".$i."</li>";
}
echo "</ol>";
Array functions
• array_pop($arr) : This function removes an element from the end of an array.
• array_push($arr, $val) : This function adds an element to the end of an array.
• array_shift($arra) : This function removes an element from the begining of an array.
• array_unshift($arr, $val) : This function adds an element to the begining of an array.
Implode and explode function
• implode() function is used to “ join elements of an array with a
string”.
• The implode function in PHP is easily remembered as “array to string”,
which simply means that it takes an array and returns a string.
• explode() function is used to “Split a string by a specified string into
pieces i.e.it breaks a string into an array.”
• explode function remembered as “string to array”.