All what you need to know about PHP from A to Z:
The PHP is a scripting language, you need to build the related web
pages with the extension .php; it is a programming language that can do all sorts of
things like: build login forms, communicate with servers and databases, evaluate data sent from a browser, build custom web content to serve the
browser, and even send and receive cookies. The php coding help, in
particularly, to build dynamic and interactive Web pages.
The fourth
basic things to know about the php Lang:
- The PHP runs at the server while the JV runs only in the browser, or at the client side.
- The PHP code is written like this: <?php echo "I'm learning PHP!"; ?>
- The file should be saved with extension .php
- The comments should be putted after //
Comparison operators in PHP:
> Greater than
< Less than
<= Less than or
equal to
>= Greater than or
equal to
== Equal to
!= Not equal to
PHP functions:
Echo: outputs strings
syntax = echo "Hello," . " " . "world" .
"!"; or echo "6*8";
Declare a variable : <?php
$myName = "elina"; ?>
Combination: <?php $myName = "elina"; echo $myName; ?>
Two and three
condition if, elseif and else:
Example for practices:- <?php $name = "Edgar"; if ($name == "Simon") { print "I know you!";} else { print "Who are you?";} ?>
- <?php $items = 10; if($items > 5) {echo "You get a 10% discount!";} else { echo "You get a 5% discount!"; } ?>
- <?php $items = 10; if ($items > 5) {echo "items is greater than 5";} elseif ($items = 5) {echo "items is equal to 5";} else {echo "items is different to 5";} ?>
- <?php switch (9) {case 0: echo 'The value is 0'; break; case 1: echo 'The value is 1'; break; case 2: echo 'The value is 2'; break; default: echo "The value isn't 0, 1 or 2"; ?>
- <?php $fruit = "Appleo"; switch ($fruit) { case 'Apple': echo "Yummy."; break; default: echo "None of the above"; } ?>
- <?php $i = 9; switch ($i) { case 0: echo '$i is 0.'; break; case 1: case 2: case 3: case 4: case 5: echo '$i is somewhere between 1 and 5.';break; case 6: case 7: echo '$i is either 6 or 7.';break; default: echo "I don't know how much \$i is.";}?>
Two kind of syntax: curly brace syntax and alternative syntax (which used when we mix html and php in the same file)
The array:
An
array is a list of items which allows you to store more than one item in only
one variable.
Example for practices:
- <?php $myArray = array("do", "re", "mi"); echo $myArray[0] // outputs "do" ?>
- <?php $moto = array("Volvo", "BMW", "Toyota");?>
The array items can be
changed and deleted by using (unset($array[2]))
The Loop: for ; while ; do/while;
foreach
Example for practices:
- <?php for ($i = 0; $i < 10; $i++) { echo $i; } // echoes 0123456789 ?>
- <?php $numbers = array(1, 2, 3); foreach($numbers as $item) { echo $item; }?>
- <?php $loopCount = 0; while ($loopCount<4){ echo "<p>Iteration number: {$loopCount}</p>"; $loopCount ++; } ?>
To add endwhile you need
Replace { with : and } with endwhile;
Example for practices:
- <?php $flipCount = 0; do { $flip = rand(0,1); $flipCount ++; if ($flip){ echo "<div class=\"coin\">H</div>"; } else { echo "<div class=\"coin\">T</div>"; } } while ($flip); $verb ="were"; $last = "flips"; if ($flipCount == 1) { $verb = "was"; $last = "flip"; } echo "<p>There {$verb} {$flipCount} {$last}!</p>"; ?>
ð ( to add the following ; css: .coin { height:
50px; width: 50px; border-radius: 25px; background-color:
grey; text-align: center; font-weight: bold; font-family: sans-serif; color: white; margin:
10px; display:
inline-block; line-height: 50px; font-size:
20px;} ) and in the <head> to add thi = <link type='text/css'
rel='stylesheet' href='style.css'/>
String functions:
Example for practices:
- <?php $myname = "otyla"; $length = strlen($myname);print $length; // .... 5 $partial = substr($myname, 0, 3); print $partial ;//...oty $uppercase = strtoupper($myname); print $uppercase;//....OTYLA $lowercase = strtolower($myname); print $lowercase;//....otyla ?>
- <?php $letposition=strpos("otyla", "f"); if ($letposition === false) { print "Sorry, no 'd' in 'otyla'"; } print $letposition ?>
Math functions:
Example for practices:
- <?php $round = round(M_PI); print $round; // ....3 ?>
- <?php $round_decimal = round(M_PI, 4);print $round_decimal; // ....3.1416 ?>
- <?php print rand(1,10); //... print a random number to the screen from 1 to10 ?>
- <?php $fav_bands = array(); array_push($fav_bands, "Maroon 5"); array_push($fav_bands, "Bruno Mars");array_push($fav_bands, "Nickelback");array_push($fav_bands, "Katy Perry"); array_push($fav_bands, "Macklemore"); print count($fav_bands); //.....5 ?>
- <?php $array = array(5, 3, 7, 1); sort($array); print join(",", $array); //...1,3,5,7 ?>
- <?php $array = array(5, 3, 7 ,1); rsort($array); print join(",", $array); //..7,5,3,1 ?>
- <?php $str = 'otyla'; echo strlen($str) ?> // …. 5
User-defined function:
Rule: function name(parameters) { statement; }
Objects in Real Life
How object-oriented programming is used in real life can be shown with a forum as an example:
Every forum user (object) has the same rights: he can log in and write (methods), can contain some settings (properties), but every user has a different name (another property).
Every user is created easily, as you create a new instance of a User class when you sign up. And as we've seen, there are some properties and methods that every instance has in common (such as logging in and writing), and there are some which are unique (such as each user's name).
And without object-oriented programming—OOP for short—this could not be done that easily. ;-)
Class in PHP:
Rule: function name(parameters) { statement; }
Example for practices:
- <?php function helloWorld() {echo "Hello world!"; } helloWorld();?> // Hello world!
- <?php function greetings($name) { echo 'Greetings '. $name ;} $n = 'fitr'; greetings($n); //...Greetings fitr ?>
- <?php function aboutMe($name,$age) { echo "Hello! My name is ".$name." and I am ".$age." years old" ;} $na = 'david'; $ag= 55; aboutMe($na,$ag); // .. Hello! My name is david and I am 55 years old ?>
Object-oriented programming
PHP is an object-oriented programming language, which means that you can
create objects, that contains variables and functions.Objects in Real Life
How object-oriented programming is used in real life can be shown with a forum as an example:
Every forum user (object) has the same rights: he can log in and write (methods), can contain some settings (properties), but every user has a different name (another property).
Every user is created easily, as you create a new instance of a User class when you sign up. And as we've seen, there are some properties and methods that every instance has in common (such as logging in and writing), and there are some which are unique (such as each user's name).
And without object-oriented programming—OOP for short—this could not be done that easily. ;-)
Class in PHP:
Example for practices:
- <?php class Person { } $teacher = new Person() $student = new Person(); ?>
- <?php class Person { public $isAlive =true; public $firstname ; public $lastname ; public $age ; //......properties } $teacher = new Person(); $student = new Person(); //....instances echo $student->isAlive; ?>
Example for practices:
- <?php //....class class Person { //......properties public $isAlive =true; public $firstname ; public $lastname ; public $age ; //...The constructor method with three parameters public function __construct($firstname, $lastname,$age ) { $this->firstname = $firstname; $this->lastname = $lastname; $this->age = $age; } } //....storing some data $teacher = new Person("ahmed", "benalai", 50); $student = new Person("imane", "kourchi", 19); //....instances echo $student->firstname; echo $teacher->firstname; ?>
- <?php //....class class Person { //......properties public $isAlive =true; public $firstname ; public $lastname ; public $age ; //...The constructor method with three parameters public function __construct($firstname, $lastname,$age ) { $this->my_name= $firstname; $this->lastname= $lastname; $this->age = $age; } } //....storing some data $teacher = new Person("Zach", "Zach", 50); $student = new Person("imane", "kourchi", 19); //....instances echo $student->my_name; echo $teacher->my_name; ?>
Example for practices:
- <?php class Person { public $isAlive = true; public $firstname; public $lastname; public $age; public function __construct($firstname, $lastname,$age) { $this->firstname =$firstname; $this->lastname =$lastname; $this->age =$age; } public function greet() { return "Hello, my name is " . $this->firstname . " " . $this ->lastname . ". Nice to meet you! :-)"; } } $teacher = new Person ("boring","12345",12345); $student = new Person ("Harambe","Kong",25); echo $teacher->isAlive; echo $student->age; echo $teacher->greet(); echo $student->greet(); ?>
- <?php //....class class Dog { //......properties public $numLegs =4; public $name; //...The constructor method with three parameters public function __construct($name) { $this->dogname= $name; } } //....storing some data $dog = new Dog ("bobi"); //....instances echo $dog->dogname; //...output =bobi ?>
- <?php //....class class Dog { //......properties public $numLegs =4; public $name; //...The constructor method with three parameters public function __construct($name) { $this->dogname= $name; } //..Add a public method to the Dog class called bark(), which returns "Woof!" public function bark() { return "Woof!"; } //..Add a public method called greet() to the Dog class. This method ought to return a nice sentence containing the $name property of the Dog, which introduces himself. public function greet() { return "Hello, my name is " .$this->dogname . ". Nice to meet you! :-)"; } } //....storing some data $dog1 = new Dog ("Barker"); $dog2 = new Dog ("Amigo"); //....instances //..call the bark() method on $dog1 and echo the result. echo $dog1->bark(); //..The last one: Call the greet() method on $dog2 and echo the result. echo $dog2->greet(); //...output =bobi ?>
Don't forget to come back from time to time, post is periodically updated.
No comments:
Post a Comment