0% found this document useful (0 votes)
7 views

PHP

Uploaded by

Raj shah
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

PHP

Uploaded by

Raj shah
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 46

PHP

Prerequisite For Learning PHP


1. HTML
• HTML is a standard markup language for documents designed to be
displayed in a web browser.
2. CSS
• CSS is a simple mechanism for adding style to web documents.
3. JAVASCRIPT
• JS is a lightweight, interpreted, or just-in-time complied programming
language with first-class functions.
What is PHP?
• PHP means Hypertext Preprocessor.
• Created by Rasmus Lerdorf in 1994.
• PHP is widely-used,open source genral-purpose Server Side Scripting
Language.
• PHP scripts are executed on server.
• Currently the latest stable version is PHP 7.3.
Programming Language Vs Scripting
Language
Programming Language Scripting Language
Has all the features needed to devlop complete Mostly used for routine tasks.It is devloped from C
applications. programming language.
The code has to be compiled before it can be The code is usually executed without compiling.
executed.
Does not need to embedded into other languages. It usually executed into other software environments.
What is PHP File?
• PHP file contain text, HTML, CSS, Javascript, and PHP code.
• PHP code is executed on the server, and the result is returned to the
browser as plain HTML.
• PHP files have execution “.php”.
What can PHP do?
• PHP can genrate dynamic page content.
• PHP can create, open, read, write,delete, and close files on the server.
• PHP can collect form data.
• PHP can send and receive cookies.
• PHP can add, delete, modify data in your database.
• PHP can be used to control user-access.
Why use PHP?
• PHP is open source and free.
• PHP was designed to work with HTML, and as such,it can be embedded
into the HTML code.
• PHP is used by 79% of all the websites whose server-side programming
language we know.
• PHP is cross platform, this means you can deploy your application on a
number of different operating systems such as windows, Linux, Mac OS
etc.
• Large community document.PHP is compatible with almost all servers
used today (Apache, IIS, etc.)
• Most web hosting servers support PHP by default unlike other languages
such as ASP that need IIS. This make PHP a cost effective choice.
How PHP works?
XAMMP
• XAMMP is a free and open-source cross-platform web server solution
stack package developed by Apache Friends, cosisting mainly of the
Apache HTTP Server, MariaDB database, and interpreters for scripts
written in the PHP and Perl Programming languages.
first.php
<?php
echo "Hello world<br>";
echo "Thank you";
?>
PHP Syntax
• In PHP file if we write only PHP code, then closing of php tag is not
necesary.
• But when we write html code in php file, then it is necesary to write
closing tag for php.
Comments In PHP
• For single line comment we use # or //.
• For multiple line comment we use /* ... */.
Variables in PHP
• Variables can be used to hold values or expressions.
• A variabe in PHP always starts with a dollar sign $.
• It must then contain a letter or underscore.
• It can then contain any of the following characters:
• abcdefghijklmnopqrstuvwxyz
• ABCDEFGHIJKLMNOP QRSTUVWXYZ
• 123456789
•_
• A variable can contain any number of letters, numbers or underscore
but must start with letter or underscore.
• valid variable name : $hari, $Hari, $_hari, $ha_ri
• Unvalid variable name : $1hari, $hari&4.
Loosly typed language PHP
• A loosly typed language is one that doesn’t need you to define the
type of variable you are declaring.
• For example, In C, we define an integer as :
• int c = 1;
• In PHP, we defined integer as :
• $PHP = 1
• In comparison to strict type language we can not define a different
kind of value in a different kind of variable. e.g. int will only have
integer value, we can’t put a string in that.
• But in PHP we can change the type of variable anytime we want.
• $PHP = 1
• $PHP = “Now this is string.”
Assign values to variable in 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

echo “My name is ‘hari’ ”


echo ‘My name is “hari” ’
echo “My name is \“hari\” ” //This print double quote inside double quote

?>
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;

if($a==$b xor $a==$c)


echo "XOR worked";

?>
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”.

You might also like