PHP
PHP
PHP
• PHP invented by Rasmus Lerdorf, 1994
• PHP Hypertext Pre-processor (earlier called, Personal Home Page)
• It is Scripting & interpreted Language
• Server side
• Case sensitive ??
• PHP files have extension “.php”
• What can be done with PHP
– PHP can generate 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
– PHP can encrypt data
PHP Syntax
• A PHP script is executed on the server, and the plain HTML result is sent back to the browser.
• A PHP script starts with <?php and ends with ?>
<?php
// PHP code goes here
?>
• A PHP file normally contains HTML tags, and some PHP scripting code.
• echo is function to display output.
• Example:-
<!DOCTYPE html>
<html>
<body>
<h1>My first PHP page</h1>
<?php
echo "Hello World!";
?>
</body>
</html>
PHP Comments
• Single Line Comment
<?php
// This is a single-line comment
foreach true
Data Types
• Scalar Data Types
– Boolean
– Integer
– Double
– String
• Compound Types
– Array
– Object
• Special Types
– Resource
– NULL
Output
echo
print ( )
printf( )
<?php
$str="RamRao";
$a=2014;
$b=18.765;
• IsSet( ) can be used to determine whether currently variable have value or not, if not NULL value
returns true else false.
• What is error_reporting? What is its level? What value one should pass to it?
Data Types
• Integer
– Single Integer type, named integer
– Up to 32 bits
• Double
– Double literals can include decimal point, an exponent(E or e) or both
– There does not need to be any digit before or after decimal point, so both .45 and 45.
are legal literals
Data Types
• String
– Character in PHP are single byte
– There is no character type
– String literals are defined either with ‘ ’ or “ ”
– In string literals (single - quoted), escape sequences are not recognized and
embedded variables are not substituted (it is called interpolation)
– In string literals (double- quoted), escape sequences are recognized and embedded
variables are replaced by their current values.
– If a double quoted string literal includes a variable name and you do not want it
interpolated, precedes the variable name(before $) with backslash.
• Boolean Type:
– TRUE and FALSE are case insensitive
– If integer expression is used in Boolean, For 0 is false and otherwise true
– If string used in Boolean context, it evaluates to false if it is either empty or “0” .
Other’s are true . This implies that the string “0.0” evaluates to True.
– The only double value that is interpreted as False is exactly 0.0, A value can be very
close to zero, but because it is not exactly zero, it will evaluate to true.
Operators
• Arithmetic Operator (+,-,*,/,%,**, ++, --)
– If operands are integer, then result is integer (what about integer division??)
– If any operand is of type double, so result will be double
– If integer results overflows, will be promoted to double
– % works with integer ( If datatype is not integer, they are coerced to integer)
– Predefined Functions
• floor(double)
• ceil(double)
• round(double)
• srand(integer) – initializes a random number generator with the parameter
• rand(two numbers) – A pseudorandom number greater than the first number and smaller than
the second
• abs(number)
• min( one or more number)
• max( one or more number )
• String Operations:
– Concatenation is with period (.)
– If str is “Mugdha” then $str{3} is ??
– Many functions to operate string
• strlen( ), strcmp( ), trim( ), ltrim( ), strtolower( ), strtoupper( ), substr( ), strops( ),chop( )
Operators
• Assignment Operator and Combined(Compound) Assignment (=, += etc)
• Relational or Comparison Operators (> , <, >=, <=, !=, ==, === , !==,
<=> , < >)
– === Identical
– !== Not Identical
– < = > Spaceship
– < > inequality
• Logical Operators (and, or, xor, !, && and ||)
• Bitwise Operator (&,|,^,~,<<,>>)
• Conditional Operator or Ternary Operator (? :)
• Conditional(assignment) Null coalescing (??)
– Example:-
• $x = expr1 ?? Expr2
• Returns the value of $x.
The value of $x is expr1 if expr1 exists, and is not NULL.
If expr1 does not exist, or is NULL, the value of $x is expr2.
Constants
• once defined cannot be changed or undefined.
• A constant is an identifier (name) for a simple value.
• no $ sign before the constant name
• To Create constants define( ) is used.
• define(name, value, case-insensitive)
• Default case-insensitive is false.
• An Arrays as constant is also possible with define.
• Constants are globally accessible.
<?php
define("PI", 3.147); # case-sensetive constant.
echo PI;
• Iterative Statements
– For and For each
– While
– Do-while
• Default Argument??
• Can one Specify Argument Type ???
• Return Type declaration:-
function addNumbers(float $a, float $b) : float {
return $a + $b;
}
Arrays
• PHP Array is Heterogeneous
• Each array element is combination of key and value
• Kays are non negative number and in ascending order
• The string keys in PHP are allowed
• PHP array can have some elements with integer keys and some with string keys
• Array Creation (Two ways, 1. Using Assignment and 2. Using Array Construct )
– Using Assignment Operator
• $list[0] = 17;
• $list[1] = 18;
• $arr[ ] = 13;
• $arr[ ] =14;
Arrays
• Array Creation (Two ways, 1. Using Assignment and 2. Using Array Construct )
– Using Assignment Operator
• Refer previous slide.
– Using Array Construct
• $arr = array ( ) # creates empty array
• $list = array(17,18,19,20)
– It is not a function, and numeric keys will be furnished by interpreter
• If you want different keys
• $list = array(1=>17,2=>18,3=>19,4=>20)
• The following statement creates an array that has the form of a hash:
• $arr = array(“Rohit”=>45, “Sachin”=>10, “Mahendra”=7, “Virendra”=309)
• PHP Array can be a mixture of number key and string keys.
• $cars = array (
array("Volvo",22,18),
array("BMW",15,13),
array("Saab",5,2),
array("Land Rover",17,15)
);
• <?php
for ($row = 0; $row < 4; $row++) {
echo "<p><b>Row number $row</b></p>";
echo "<ul>";
for ($col = 0; $col < 3; $col++) {
echo "<li>".$cars[$row][$col]."</li>";
}
echo "</ul>";
}
?>
• Ref:- https://www.w3schools.com/php/php_arrays_multidimensional.asp