Professional Documents
Culture Documents
It is powerful enough to be at the core of the biggest blogging system on the web
(WordPress)!
It is deep enough to run large social networks!
It is also easy enough to be a beginner's first server side language!
Why PHP?
● PHP runs on various platforms (Windows, Linux, Unix, Mac OS X, etc.)
● PHP is compatible with almost all servers used today (Apache, IIS, etc.)
● PHP supports a wide range of databases
● PHP is free. Download it from the official PHP resource: www.php.net
● PHP is easy to learn and runs efficiently on the server side
What is a PHP File?
● PHP files can 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 extension ".php"
?>
The default file extension for PHP files is ".php".
A PHP file normally contains HTML tags, and some PHP scripting code.
<!DOCTYPE html>
<html>
<body>
<h1>My first PHP page</h1>
<?php
echo "Hello World!";
?>
</body>
</html>
PHP Case Sensitivity
In PHP, keywords (e.g. if, else, while, echo, etc.), classes, functions, and user-defined functions are not case-
sensitive. However; all variable names are case-sensitive!
In the example below, all three echo statements below are equal and legal:
Example
<!DOCTYPE html>
<html><body>
<?php
?>
</body></html>
Example
$COLOR is not same as $color:
<!DOCTYPE html>
<html><body>
<?php
$color = "red";
echo "My car is " . $color . "<br>";
echo "My house is " . $COLOR . "<br>";
echo "My boat is " . $coLOR . "<br>";
?>
</body></html>
PHP Comments
A comment in PHP code is a line that is not executed as a part of the program. Its
only purpose is to be read by someone who is looking at the code.
/* This is a
multi-line comment */
PHP Variables
Variables are "containers" for storing information.
In PHP, a variable starts with the $ sign, followed by the name of the
variable:
Example
$x = 5;
$y = "John“
When you assign a text value to a variable, put quotes around the value
PHP Variables
A variable can have a short name (like $x and $y) or a more descriptive
name ($age, $carname, $total_volume).
The PHP echo statement is often used to output data to the screen.The following example will show
how to output text and a variable:
Example
$txt = "Amrutvahini";
Or
$txt = "Amrutvahini";
$y = 5;
echo $x + $y;
o/p 10
PHP is a Loosely Typed Language
In the example above, notice that we did not have to tell PHP which
data type the variable is.
echo $y;
PHP supports the following data types:
● String
● Integer
● Float (floating point numbers - also called double)
● Boolean
● Array
● Object
● NULL
● Resource
Get the Type
To get the data type of a variable, use the var_dump() function.
Example
The var_dump() function returns the data type and the value:
$x = 5;
var_dump($x);
o/p int(5)
<!DOCTYPE html> O/P
<html><body> int(5)
string(4) "John"
<pre> float(3.14)
bool(true)
<?php
array(3) {
var_dump(5); [0]=>
int(2)
var_dump("John"); [1]=>
var_dump(3.14); int(3)
[2]=>
var_dump(true); int(56)
}
var_dump([2, 3, 56]);
var_dump(NULL);
?></pre></body></html>
Assign String to a Variable
echo $x;
You can assign the same value to multiple variables in one line:
With PHP, there are two basic ways to get output: echo and print.
echo and print are more or less the same. They are both used to output
data to the screen.
echo has no return value while print has a return value of 1 so it can be
used in expressions.
echo can take multiple parameters (although such usage is rare) while print
can take one argument.
<body>
PHP is Fun!
Hello world!
<?php I'm about to learn PHP!
This string was made with multiple
echo "<h2>PHP is Fun!</h2>"; parameters.
echo "This ", "string ", "was ", "made ", "with multiple parameters.";
?>
</body>
</html>
<!DOCTYPE html>
O/P
<html><body>
<?php Learn PHP
Study PHP at W3Schools.com
$txt1 = "Learn PHP"; 9
$txt2 = "W3Schools.com";
$x = 5;
$y = 4;
echo "<h2>" . $txt1 . "</h2>";
echo "Study PHP at " . $txt2 .
"<br>";
echo $x + $y;
?></body></html>
The PHP print Statement
The print statement can be used with or without parentheses: print or print().
<!DOCTYPE html>
<html>
<body>
<?php
?>
</body>
</html>
<!DOCTYPE html>
O/P
<html><body>
Learn PHP
<?php
Study PHP at W3Schools.com
9
$txt1 = "Learn PHP";
$txt2 = "W3Schools.com";
$x = 5;
$y = 4;
print $x + $y;
?></body></html>
PHP Data Types
PHP String
A string is a sequence of characters, like "Hello world!".
A string can be any text inside quotes. You can use single or double quotes:
PHP Integer
An integer data type is a non-decimal number between -2,147,483,648
and 2,147,483,647.
In the following example $x is a float. The PHP var_dump() function returns the
data type and value:
$x = 10.365;
var_dump($x);
PHP Boolean
$x = true;
PHP Array
?> </body></html>
PHP Operators
Operators are used to perform operations on variables and values.
● Arithmetic operators
● Assignment operators
● Comparison operators
● Increment/Decrement operators
● Logical operators
● String operators
● Array operators
● Conditional assignment operators
PHP Arithmetic Operators
The PHP arithmetic operators are used with numeric values to perform
common arithmetical operations, such as addition, subtraction, multiplication
+ Addition $x + $y Sum of $x
and $y
- Subtraction $x - $y
Difference of $x and $y
* Multiplication $x * $y
Product of $x and $y
/ Division $x / $y
Quotient of $x and $y
PHP Assignment Operators
The PHP assignment operators are used with numeric values to write a value
to a variable.The basic assignment operator in PHP is "=". It means that the
left operand gets set to the value of the assignment expression on the right.
x=y x=y The left operand gets set to the value of the
expression on the right
x += y x=x+y Addition
x -= y x=x-y Subtraction
x *= y x=x*y Multiplication
x /= y x=x/y Division
PHP Comparison Operators
The PHP comparison operators are used to compare two values (number or string):
=== Identical $x === $y Returns true if $x is equal to $y, and they are of
the same type
!== Not identical $x !== $y Returns true if $x is not equal to $y, or they
are not of the same type
$x >= $y
$x <= $y
<=> Spaceship
$x <=> $y
Returns an integer less than, equal to, or greater than zero, depending on if $x is
less than, equal to, or greater than $y. Introduced in PHP 7.
PHP Increment / Decrement Operators
The PHP increment operators are used to increment a variable's value.
?>
</body>
</html>
PHP - The if...else Statement
<?php
The if...else statement executes some
$num=12;
code if a condition is true and another
code if that condition is false. if($num%2==0){
code to be executed if
condition is false;
}
PHP - The if...elseif...else Statement <?php
$marks=69;
The if...elseif...else statement executes
different codes for more than two conditions. if ($marks<33){
echo "fail"; }
Syntax else if ($marks>=34 && $marks<50) {
if (condition) { echo "D grade"; }
code to be executed if this else if ($marks>=50 && $marks<65) {
condition is true;
echo "C grade"; }
} elseif (condition) {
else if ($marks>=65 && $marks<80) {
code to be executed if first echo "B grade"; }
condition is false and this condition
is true; else if ($marks>=80 && $marks<90) {
Syntax
switch (expression) {
case label1:
//code block
break;
case label2:
//code block;
break;
default:
//code block }
<html> break;
<body> default:
<?php echo "Your favorite color is neither red, blue,
$favcolor = "red"; nor green!";
switch ($favcolor) { }
break; </html>
case "blue":
echo "Your favorite color is blue!";
break;
case "green":
echo "Your favorite color is green!";
PHP Loops
Often when you write code, you want the same block of code to run over and over again a
certain number of times. So, instead of adding several almost equal code-lines in a script, we
can use loops.
Loops are used to execute the same block of code again and again, as long as a certain
condition is true.
● while - loops through a block of code as long as the specified condition is true
● do...while - loops through a block of code once, and then repeats the loop as long as the
specified condition is true
● for - loops through a block of code a specified number of times
● foreach - loops through a block of code for each element in an array
The PHP while Loop
The while loop executes a block of code as long as the specified condition is true.
<!DOCTYPE html>
<html><body>
<?php
$i = 1;
echo $i;
$i++;
?>
</body></html>
The break Statement
With the break statement we can stop the loop even if the condition is still true:
<!DOCTYPE html>
<html><body>
<?php
$i = 1;
if ($i == 3) break;
echo $i;
$i++;
?> </body></html>
The PHP do...while Loop
The do...while loop will always execute the block of code at least once, it will then check the
condition, and repeat the loop while the specified condition is true.
<!DOCTYPE html>
<html><body>
<?php
$i = 1;
do {
echo $i;
$i++;
?></body></html>
The PHP for Loop
The foreach loop - Loops through a block of code for each element in an array or
each property in an object.
● Normally, when a function is completed/executed, all of its variables are deleted. However, sometimes we
want a local variable NOT to be deleted. We need it for a further job.To do this, use the static keyword when
you first declare the variable:
<html>
<body>
<?php
function myTest() {
static $x = 0;
echo $x;
$x++;
}
myTest();
echo "<br>";
myTest();
echo "<br>";
myTest();
?>
</body>
</html>
PHP $ and $$ Variables
● The $var (single dollar) is a normal variable with the name var that stores any
value like string, integer, float, etc.
● The $$var (double dollar) is a reference variable that stores the value of the
$variable inside it.
● <?php
● $x = "abc";
● $$x = 200;
● echo $x."<br/>";
● echo $$x."<br/>";
● echo $abc;
● ?>
Type Juggling in PHP
-Type juggling means dealing with a variable type. In PHP a variable type is the context in which
it is used. If an integer value is assigned to a variable, it becomes an integer. If a string value is
assigned to the variable, it becomes a String.
-PHP does not require (or it support) explicit type definition in variable declaration; a variable's
type is determined by the context in which the variable is used.
-PHP variable types may be modified without any explicit conversion process.
This conversion process, whether explicit or implicit, is known as type juggling.
<?php
$a=1; #int
$b='2'; #string
$c=$a+$b; #become int
echo "$c",$c;
<?php
<?php
$total = 100; $total = 100;
$qty = "20 pieces";
$qty = "20"; $total = $total + $qty;
$total = $total + $qty;
echo $total; // 120
echo $total; // 120
?>
Type Casting in PHP
● Typecasting is a way to convert one data type variable into different data
types. A type can be cast by inserting one of the casts in front of the variable