History of PHP:
Variables
PHP is a server side scripting language used for making simple and complex web applications. It was originally designed by Rasmus Lerdorf using C Language in 1994-95. Initially it was made for tracking personal information with a name as Personal Home Page/Form Interpreter (PHP/FI).
1. In 1997, Andi Gutmans and Zeev Suraski from Israel contributed significantly with an aim to run ecommerce site, It was released with version PHP 3.0 and The acronym was recursively named as Hypertext preprocessor.
2. In 2000, PHP 4 was released with Zend Engine 1.0. This Engine had provided higher speed and reliability over PHP 3.0 It added features such as COM support for windows, Boolean type, extended Object oriented Programming features and many more.
3. In 2004, PHP 5 was released with features such as Exception Handling Mechanism, SOAP Support, iterators, Support for database such as SQLite and MySQL etc.
4. In 2010, PHP 6 was abandoned and PHP 5.3.2, was transformed in PHP 6.
5. In 2014-15 PHP 7 was released with major security and performance improvement by Dmitry Stogov, Xinchen Hui and Nikita Popov.
Use of PHP
1. Communicate with Web Server such as apache, Nginx etc
2. Develop Dynamic websites.
3. Communicate with database server MySQL, PostgreSQL ,MariaDB etc.
4. Protect direct access of web pages.
5. It can be used to send and receive emails.
6. Data can be encrypted using mcrypt function
7. To use database in web application
8. For performing all types of file related operation
9. Can be used to make session based web application
10. Used by Content Management framework like joomla , Wordpress etc.
Characteristics of PHP
1. Easy to learn hence it is a very simple.
2. It is an open source hence money efficient and it also has efficient coding style.
3. Platform independent server side Language
4. It is flexible and secure.
5. Availability of rich support.
Syntax of PHP
1. It begins with <? php tag and ends with ?> tag
2. Just Like C every statement ends with semicolon “;”
3. PHP file can be recognized with .php extention
4. PHP file consist of HTML and PHP Scripts.
Example: first.php
<!DOCTYPE html>
<html>
<body>
<h1>PHP First Example</h1>
<?php
echo "Hello Students!"; // This double forward slash used for comment and echo for display.
?>
</body>
</html>
Output:
PHP First Example
Hello Students!
Variables
It is a token used as identifier for storing values that can be changed at run time. It is written by ‘$’ sign then name to identify.
Syntax: $variblename = value;
Example:
<?php
$var = “Hello Students!!”;
echo “Display String : $var”;
?>
Output:
Hello Students!!
Types of Variable:
In PHP there is no need to declare types of variable as it is automatically converted when needed.
It is explained with below example
Example:
<?php
$var='Hello World!';//assign value using "="
$var1=5;
$var2=2;
echo $var.$var1;// append or concatenate using "." and $var1 behave as string
echo $var2+$var1;//$var1 behave as number
?>
Output:
52
7
Rules of variable declaration
1. A variable name must start with $ sign followed by character or underscore
2. It cannot be begin with number
3. It is case-sensitive means $A and $a both are different.
4. It consists of alpha-numeric characters and underscore.
Datatypes
Following are the types of data supported by PHP:
Integer: Any Numeric Whole Number Ex: 14,2458,4579
String : Any set of Characters Ex : address, name, age
Array: Collection of items viz objects, string , integer etc.
Float: Fraction values Ex: 4.75
Boolean: Support either true or false
Object: Any Entity reserved memory with set of functions and variables.
Null: Does not have any value.
Resource: For Handling file data or database data this variables are used. Ex $fp = fopen("index.php",'r');
Expressions and operators
Expressions are values returned from calculation done from any statement.
Example:
<?php
$a = 5; //Expression with assigning value 5 to $a variable.
function foo(){
return 5;
}
$b = foo();//Expression with function foo will return value which will be assigned to $b after calculation.
?>
Operators are used to perform operations.
Types of Operators:
Ø Arithmetic Operators : Plus (+), minus(-), multiply(*), division(/), mod(%), exponent(**)
Example:
<?php
$a = 5; $b = 5;
$c=$a+$b;
echo “Addition: ”.$c.”\n”;
$c=$a-$b;
echo “Subtraction: ”.$c.”\n”;
$c=$a*$b;
echo “Multiplication: ”.$c.”\n”;
$c=$a**$b;
echo “Exponentiation: ”.$c.”\n”;
$c=$a%$b;
echo “Modulus: ”.$c.”\n”;
?>
Output:
10
0
25
3125
0
Logical or Relational Operators: Logical Bitwise & (and), Logical Bitwise | (or), Logical Bitwise ^ (xor), Logical &&(and), Logical || (or), !(not for compare),~ (not for bitwise)
Example:
<?php
$a = 100;
$b = 101;
$d = 0;
$c = $a & $b;
echo "AND Bitwise is :".$c."\n";
$c = $a | $b ;
echo "OR Bitwise is :".$c."\n";
$c = !($d) ;//Use for changing true to false and false to true also
echo "NOT is :".$c."\n";
$c = $a ^ $b ;
echo "XOR Bitwise is :".$c."\n";
// Use for comparing more than one conditions
if(($a==100) && ($b==101))
echo "Only Both of the conditions are true"."\n";
if(($a==100) || ($b==2))
echo "Either of the conditions or both are true"."\n";
?>
Output:
AND Bitwise is :100
OR Bitwise is :101
NOT is :1
XOR Bitwise is :1
Only Both of the conditions are true
Either of the conditions or both are true
Comparison Operators: equal to (==), not equal to (!=), less than equal to (<=), greater than equal to (>=), Identical(===), Not Identical (!==), Not equal to (<>)
Example:
<?php
$a = 100;
$b = 150;
$c= “100”;
if($a==$b)
echo “\n”.“It is equal : condition is true”; else echo “\n”. “It is not equal : condition is false”;
if($a!=$b)
echo “\n”. “It is not equal : condition is true”; else echo “\n”. “It is equal : condition is false”;
if($a<=$b)
echo “\n”. “It is less than or equal to : condition is true”; else echo “\n”. “It is greater than : condition is false”;
if($a>=$b)
echo “\n”. “It is greater than or equal to : condition is true”; else echo “\n”. “It is less than : condition is false”;
if($a===$c)// Though values are 100 but one is string and one is number hence not identical
echo “\n”. “It is identical : condition is true”; else echo “\n”. “It is not identical : condition is false”;
?>
Output:
It is not equal: condition is false
It is not equal: condition is true
It is less than or equal to : condition is true
It is less than: condition is false
It is not identical: condition is false
Conditional or Ternary Operators: In a single statement condition can be given.
Syntax: $var = (condition)? value1: value2;
If condition is true value1 will be assigned to $var else value2 will be assigned.
Example:
<?php
$x = 100;
$y=150;
echo ($x >$y ) ? ‘x is greater than y' : 'x is smaller than y';
?>
Output:
x is smaller than y
Array Operators or Set Operator: Union (+), Equality (==), Inequality (!== or <>), Identity(===), Non Identity (!==)
Example:
<?php
$a1 =
array("V1" => "cycle", "V2" => "motorcycle");;
$a2=
array("V3" => "truck", "V4" => "auto");;
var_dump($a1 + $a2); //var_dump function gives information about variable.
var_dump($a1 == $a2) + "\n";
var_dump($a1 != $a2) + "\n";
var_dump($a1 <> $a2) + "\n";
var_dump($a1 === $a2) + "\n";
var_dump($a1 !== $a2) + "\n";
?>
Output:
array(4) {
["V1"]=>
string(5) "cycle"
["V2"]=>
string(10) "motorcycle"
["V3"]=>
string(5) "truck"
["V4"]=>
string(4) "auto"
}
bool(false)
bool(true)
bool(true)
bool(false)
bool(true)
Increment/Decrement Operators:
Pre-Increment (++$x): First Increment then return value
Pre-Decrement (--$x): First Decrement then return value
Post-Increment ($x++): First return value then Increment
Post-Decrement ($x--): First return value then Decrement
Example:
<?php
$a = 10;
echo ++$a, " First increments then display \n";
echo $a, "\n";
$a = 10;
echo $a++, " First display then increments \n";
echo $a, "\n";
$a = 10;
echo --$a, " First decrements then display \n";
echo $a, "\n";
$a = 10;
echo $a--, " First display then decrements \n";
echo $a;
?>
Output:
11 First increments then display 11
10 First display then increments 11
9 First decrements then display 9
10 First display then decrements 9
String Operators: Dot (.), Concatenate and assignment (.=)
Ø Example:
<?php
$a = "Hello";
$b = “Student”;
echo $a . $b , "\n"; // Dot operator combines both strings
$a .= $b ; /*Dot operator combine both string “Hello Student” and assigned to $a. New Value of $a is “Hello Student” and not “Hello” */
echo $a;
?>
Output:
Hello Student
Hello Student
Spaceship Operators: It is introduced in PHP 7 its notation is ‘<=>’ and it will return values on comparison. Following are the return values:
Equal: return 0
Right operand in greater: return -1
Left operand is greater: return 1
Example:
<?php
$a = "Hello";
$b = “Student”;
echo $a <=> $b;
echo "\n";
echo $b <=> $a;
echo "\n";
echo $a <=> $a;
?>
Output:
-1
1
0
Constants
Values which will never change are called as constants.
Syntax:
define (name, value, case-insensitive)
where
name: constant name , value: Constant value, if case insensitive : true else false
Example:
<?php
define("PI",”3.14”, false);
echo PI;
?>
Output:
3.14
Strings:
It is a sequence of characters. Example: “Student” this can be easily assigned to variable using double quotes, $a=”Student”;
PHP has provided following inbuilt functions for doing string related operations:
1. strlen(): return the length of the string.
2. str_word_count(): return the number of words in a
sentence.
3. strrev(): reverse of String
4. strpos(): position of specified text in a sentence.
5. tr_replace(): replace words or character with given
one.
6. echo(): display string.
7. chr(): returns value of ascii code.
8. strncmp(): compare two strings. It is case sensitive.
9. strtolower(): convert character from upper to lower case.
10. strtoupper():convert character from lower to upper
case.
For more detail list of String functions visit website: www.w3schools.com/php/php_ref_string.asp
Example:
<?php
$a ="Students of college";
echo "length is ". strlen($a)."\n";
echo "Number of Words are : ".str_word_count($a)."\n";
echo "Reverse is :". strrev($a)."\n";
echo "Position of college is :". Strpos("college",$a)."\n";
echo "Replace c with a : ". str_replace("c","a",$a)."\n";
echo "Use of echo is to Display string"."\n";
echo "Compare with string Students of institute: ".strncmp("Students of institute",$a,5)."\n";//compare first 5 char
echo "Lower ABC is :".strtolower("ABC")."\n";
echo "Upper abc is :".strtoupper("abc")."\n";
echo "value of ascii code 52 is:".chr(52);// returns ascii value
?>
Output:
length is 19
Number of Words are : 3
Reverse is :egelloc fo stnedutS
Position of college is :
Replace c with a : Students of aollege
Use of echo is to Display string
Compare with string Students of institute: 0
Lower ABC is :abc
Upper abc is :ABC
Return value of ascii code 52 is:4
No comments:
Post a Comment