Unit - 3 PHP - 1
Unit - 3 PHP - 1
• ASP
• ASP.NET
• PHP
• JSP
• Servlet
<?php
echo "Hello World";
?>
</body>
</html>
•Install XAMPP
•Start Apache & MySQL services
•Save the file as .php
•Copy file.php to htdocs (C:/XAMPP/htdocs)
•Open browser with http://localhost/file.php
Client – Server - Client
Client – Server - Client
Render as HTML
PHP Comments
<!DOCTYPE html>
<html>
•In PHP, we use <body>
<?php
// single-line comment // This is a single-line comment
</body>
</html>
PHP Variables
•> Variables are used for storing values, like text strings, numbers or arrays.
•> When a variable is declared, it can be used over and over again in your
script.
•> All variables in PHP start with a $ sign symbol.
• > In PHP, a variable does not need to be declared before adding a value to
it.
•> PHP automatically converts the variable to the correct data type,
depending on its value.
<?php
$txt = "Hello world!";
$x = 5;
$y = 10.5;
?>
PHP Variables
• A variable starts with the $ sign, followed by the name of the variable
• A variable name must start with a letter or the underscore character
• A variable name cannot start with a number
• A variable name can only contain alpha-numeric characters and
underscores (A-z, 0-9, and _ )
• Variable names are case-sensitive ($age and $AGE are two different
variables)
• PHP is a Loosely Typed Language
<?php
$txt = “RECW";
echo "I love $txt!";
?>
PHP Concatenation
•> The concatenation operator (.) is used to put
two string values together.
•> To concatenate two string variables together,
use the concatenation operator:
<?php
$txt = "RECW";
echo "I love" . $txt . "!";
?>
Variables Scope
• In PHP, variables can be declared anywhere in the
script.
• The scope of a variable is the part of the script
where the variable can be referenced/used.
• PHP has three different variable scopes:
– local
– global
– static
Local Scope
<!DOCTYPE html>
<html>
<body>
<?php
function myTest() {
$x = 5; // local scope
echo "<p>Variable x inside function is: $x</p>";
}
myTest();
</body>
</html>
Global Scope
<!DOCTYPE html>
<html>
<body>
<?php
$x = 5; // global scope
function myTest() {
// using x inside this function will generate an error
echo "<p>Variable x inside function is: $x</p>";
}
myTest();
</body>
</html>
Global Scope
The global keyword is used to access a global variable from within a function.
<!DOCTYPE html>
<html>
<body>
<?php
function myTest() {
$x = 5; // local scope
echo "<p>Variable x inside function is: $x</p>";
}
myTest();
</body>
</html>
Global Scope
PHP also stores all global variables in an array called $GLOBALS[index].
The index holds the name of the variable.
This array is also accessible from within functions and can be used to update global variables
directly.
<!DOCTYPE html>
<html>
<body>
<?php
$x = 5;
$y = 10;
function myTest() {
$GLOBALS['y'] = $GLOBALS['x'] + $GLOBALS['y'];
}
myTest();
echo $y;
?>
</body>
</html>
Static Scope
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:
<!DOCTYPE html>
<html>
<body>
<?php
function myTest() {
static $x = 0;
echo $x;
$x++;
}
OUT PUT
myTest();
0
echo "<br>";
myTest(); 1
echo "<br>"; 2
myTest();
?>
</body>
</html>
echo and print Statements
• two basic ways to get output: echo and print.
• echo and print are more or less the same.
• 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.
• echo is marginally faster than print.
• The print statement can be used with or without
parentheses: print or print().
<?php
print "<h2>PHP is Fun!</h2>";
print "Hello world!<br>";
print "I'm about to learn PHP!";
?>
PHP Data Types
Variables can store data of different types, and different data
types can do different things.
PHP supports the following data types:
• String
• Integer
• Float (floating point numbers - also called double)
• Boolean
• Array
• Object
• NULL
• Resource (reference to functions and resources external to
PHP)
PHP String
• A string can be any text inside quotes. You can use
single or double quotes
<!DOCTYPE html>
<html>
<body>
<?php
$x = "Hello world!";
$y = 'Hello world!';
echo $x;
echo "<br>";
echo $y;
?>
</body>
</html>
PHP String Functions
• strlen() - Return the Length of a String
• str_word_count() - Count Words in a String
• strrev() - Reverse a String
• strpos() - Search For a Text Within a String
• str_replace() - Replace Text Within a String
<!DOCTYPE html>
<html>
<body>
<?php
echo strlen("Hello world!")."<br>";
echo str_word_count("Hello world!")."<br>";
echo strrev("Hello world!")."<br>";
echo strpos("Hello world!","world")."<br>";
echo str_replace("world","RECW","Hello world!");
?>
</body>
</html>
PHP Integer
• An integer data type is a non-decimal number between
-2,147,483,648 and 2,147,483,647.
• An integer must have at least one digit
• An integer must not have a decimal point
• An integer can be either positive or negative
• Integers can be specified in: decimal (base 10), hexadecimal (base
16), octal (base 8), or binary (base 2) notation
• The PHP var_dump() function returns the data type and value:
<!DOCTYPE html>
<html>
<body>
<?php
$x = 5985;
OUTPUT
var_dump($x);
int(5985)
?>
</body>
</html>
PHP Float
• A float (floating point number) is a number with a decimal point or
a number in exponential form.
<!DOCTYPE html>
<html>
<body>
<?php
$x = 10.365;
var_dump($x);
?>
</body>
</html>
OUTPUT
float(10.365)
PHP Boolean
• A Boolean represents two possible states: TRUE or FALSE.
$x = true;
$y = false;
PHP Array
• An array stores multiple values in one single variable.
<!DOCTYPE html>
<html>
<body>
<?php
$cars = array("Volvo","BMW","Toyota");
var_dump($cars);
?>
</body>
</html>
OUTPUT
array(3) { [0]=> string(5) "Volvo" [1]=> string(3) "BMW" [2]=> string(6) "Toyota" }
PHP Object
• Classes and objects are the two main aspects of object-oriented
programming.
• A class is a template for objects, and an object is an instance of a
class.
• When the individual objects are created, they inherit all the
properties and behaviors from the class, but each object will have
different values for the properties.
<!DOCTYPE html>
<html>
<body>
<?php
class Car {
public $color;
public $model;
public function __construct($color, $model) {
$this->color = $color;
$this->model = $model;
}
public function message() {
return "My car is a " . $this->color . " " . $this->model . "!";
}
}
<!DOCTYPE html>
<html>
<body>
<?php
$x = "Hello world!";
$x = null;
var_dump($x);
?>
</body> OUTPUT
</html> NULL
PHP Operators
<!DOCTYPE html>
<html>
<body>
<?php
$x = 10;
echo ++$x;
?>
</body>
</html>
PHP Logical Operators
<!DOCTYPE html>
<html>
<body>
<?php
$x = 100;
$y = 50;
</body>
</html>
PHP String Operators
<!DOCTYPE html>
<html>
<body>
<?php
$txt1 = "Hello";
$txt2 = " world!";
$txt1 .= $txt2;
echo $txt1;
?>
</body>
</html>
PHP Array Operators
Conditional Assignment Operators
<!DOCTYPE html>
<html>
<body>
<?php
$x=100;
$y=20;
echo $st=($x>$y) ? "$x is greater" : "$y is greater";
?>
</body>
</html>
PHP Conditional Statements
•> if statement - use this statement to execute
some code only if a specified condition is true
•> if...else statement - use this statement to
execute some code if a condition is true and
another code if the condition is false
•> if...elseif....else statement - use this statement to
select one of several blocks of code to be executed
•> switch statement - use this statement to select
one of many blocks of code to be executed
PHP - The if Statement
if (condition) {
code to be executed if condition is true;
}
<!DOCTYPE html>
<html>
<body>
<?php
$t = date("H");
</body>
</html>
PHP - The if...else Statement
if (condition) {
code to be executed if condition is true;
} else {
code to be executed if condition is false;
}
The if...elseif...else Statement
if (condition) {
code to be executed if this condition is true;
} elseif (condition) {
code to be executed if first condition is false
and this condition is true;
} else {
code to be executed if all conditions are false;
}
PHP switch Statement
switch (n) {
case label1:
code to be executed if n=label1;
break;
case label2:
code to be executed if n=label2;
break;
case label3:
code to be executed if n=label3;
break;
...
default:
code to be executed if n is different from all labels;
}
<!DOCTYPE html>
<html>
<body>
<?php
$favcolor = "red";
switch ($favcolor) {
case "red":
echo "Your favorite color is red!";
break;
case "blue":
echo "Your favorite color is blue!";
break;
case "green":
echo "Your favorite color is green!";
break;
default:
echo "Your favorite color is neither red, blue, nor green!";
}
?>
</body>
</html>
PHP Loops
In PHP, we have the following loop types:
•while - loops through a block of code as long as the specified
condition is true
<?php
$x = 1;
while($x <= 5) {
echo "The number is: $x <br>";
$x++;
}
?>
</body>
</html>
PHP do while Loop
do {
code to be executed;
} while (condition is true);
<!DOCTYPE html>
<html>
<body>
<?php
$x = 1;
do {
echo "The number is: $x <br>";
$x++;
} while ($x <= 5);
?>
</body>
</html>
PHP for Loop
for (init counter; test counter; increment counter) {
code to be executed for each iteration;
}
<!DOCTYPE html>
<html>
<body>
<?php
for ($x = 0; $x <= 10; $x++) {
echo "The number is: $x <br>";
}
?>
</body>
</html>
PHP foreach Loop
The foreach loop works only on arrays, and is used to loop through each
key/value pair in an array.
foreach ($array as $value) {
code to be executed;
}
<!DOCTYPE html>
<html>
<body>
<?php
$colors = array("red", "green", "blue", "yellow");
</body>
</html>