View
71
Download
1
Category
Preview:
Citation preview
is created by Rasmus Lerdorf in 1994.
stands for “Personal Home Page”.
is influenced by C, C++, Java and TCL.
competes with JSP, ASP.NET, Perl and etc.
is used to create FB, Wikipedia, WP and etc.
is used in 60-70% of the world’s websites.
PHP is free software under the PHP License.
Intro
Integers are platform-dependent equivalent to the C long type.
Data Types
Floating point numbers are also stored in a platform-specific range.
Boolean type is similar to the native Boolean types in Java and C++.
Resource type represent references to external sources.
Arrays can contain elements of any type that PHP can handle
Integer . Double . Booleans . NULL . String . Array . Object . Resource
Data Types:
Variable
Variables: <?php $number = 666; ?>
Constants: <?php define(“Name”, “Value”); ?>
Strings: <?php
$string = “Text”;
$string2 = $string . “Extra”;
?>
In Strings: <?php echo “Variable = $variable”; ?>
Casting: <?php $number = (int) $string; ?>
+
-
*
/
%
++
--
Operators
Arithmetic . Comparison . Logical . Assignment==
!=
>
<
>=
<=
And
Or
&&
||
!
=
+=
-=
*=
/=
%=
$array[0], $array[1], $array[3], …
Array
Numeric:
$person[“name”], $person[“surname”], $person[“age”]
Associative:
$outside[key] = $inside;
Multidimensional:
Functionfunction write($message) {
echo $message;
}
function add($num1, $num2) {
return $num1 + $num2;
}
Normal functions are not first-class.
User-defined functions can be created at any time without prototype.
Functions can be defined inside code blocks, permitting a
run-time decision as to whether or not a function should be defined.
Support for true anonymous functions is not exist in PHP.
Closurefunction getAdder($x)
{
return function($y) use ($x)
{
return $x + $y;
};
}
$adder = getAdder(8);
echo $adder(2); // prints "10"
OOP
class Student {
Private $name;
Private $surname;
Private $No;
public function getName() {
return $this->name;
}
}
Define Class:
$student = new Student();
echo $student->getName();
Create Object:
OOP
class Student {
Private $name;
Private $surname;
Private $No;
public function __construct() {
// codes…
}
}
Constructor:
class Student {
Private $name;
Private $surname;
Private $No;
public function __destruct() {
// codes…
}
}
Destructor:
OOP
interface Mail {
public function sendMail();
}
class Report implements Mail {
public function sendMail() {
// implementation…
}
}
Interface:
abstract class Sample {
abstract function method1() ;
function method2() {
// implementation…
}
}
$sample = new Sample(); // Err
Abstract Class:
OOP
class Sample {
public static $xxx;
static function abc() {
// implementation…
}
}
echo Sample::xxx;
echo Sample::abc();
Statics:
class Sample {
final public function abc() {
// final implementation
}
}
Note:
Properties cannot be declared final
Final:
Exception
try {
// Codes to run
} catch(Exception $e) {
// Process exception
} finally { // PHP 5.4
// Do anyway!
}
Syntax:throw new Exception($message);
Throw
echo $e->getMessage();
Catch
Exception
$x = $_GET[“number”];
try {
if($x>1000)
throw new Exception(“Too big!”);
echo “The number is: “ . $x;
} catch (Exception $e) {
echo “Error: “ . $e->getMessage();
}
Example:
Cookie
setcookie(name, value, expire, path, domain, security);
Create:
$_COOKIE["name"];
Access:
setcookie(“Name", “Hasan Teymuri", time()+3600, "/","", 0);
Example:
Session
session_start();
Get Started:
$_SESSION [“Name"] = “Vahid Dehghani”;
Create:
echo $_SESSION[“Name”];
Access:
The End
Main Source: http://tutorialspoint.com
Special thanks to Google, Wikipedia
and who has invented copy-and-paste!
Written By
Milad Rahimi [www.MiladRahimi.com]
Recommended