PHP stands for "Hypertext Preprocessor", it is a server scripting language.
What Can PHP do?
- 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 is used to control user-access
- PHP can encrypt data
1> Basic PHP Syntax
1 <? PHP 2 // PHP code goes here 3 ?>
2> Comments in PHP
1<?PHP2 //This is a single-line comment3 4 #This is also a single-line comment5 6 /*7 This is a multiple-lines comment block8 That spans over multiple9 LinesTen */ One A //You can also use comments to leave out parts of a code line - $x= 5/*+*/+ 5; - Echo $x; the?>
3> Case Sensitivity
In PHP, any keywords (e.g. if, else, while, echo, etc), classes, functions, and user-defined functions is not case- Sensitive.
1 <? PHP 2 ECHO "Hello world!<br>"; 3 Echo "Hello world!<br>"; 4 EcHo "Hello world!<br>"; 5 ?>
however; All variable names is case-sensitive.
1 <? PHP 2 $color = "Red"; 3 Echo $color . "<br>"; 4 Echo $COLOR . "<br>"; 5 Echo $coLOR . "<br>"; 6 ?>
4> PHP Variables
Rules for 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 is case-sensitive ($age and $AGE are, different variables)
PHP is a loosely Typed Language
PHP Variables Scope
PHP has three different variable scopes:
PHP also stores all global variables in an array called $GLOBALS [index]. The index holds the name of the variable.
1<?PHP2 $x= 5;3 $y= 10;4 5 functionmyTest () {6 $GLOBALS[' y '] =$GLOBALS[' X '] +$GLOBALS[' Y '];7 }8 9 myTest ();Ten Echo $y;//outputs One?>
5> PHP Echo and Print statements
Echo and print is more or less the same. They is both used to output data to the screen.
The differences is small:
- echo have no return value while print have a return value of 1 so it can be used in expressions.
- Echo can take multiple parameters (although such usage are rare) while the print can take one argument.
- Echo is marginally faster than print.
Echo Display Text
1 <? PHP 2 Echo "; 3 Echo "Hello world!<br>"; 4 Echo "I ' m about to learn Php!<br>"; 5 Echo "This", "string", "is", "made", "with multiple parameters." ; 6
Echo Display Variables
1<?PHP2 $txt 1= "Learn PHP";3 $txt 2= "W3schools.com";4 $x= 5;5 $y= 4;6 7 Echo"$txt 1;8 Echo"Study PHP at$txt 2<br> ";9 Echo $x+$y;Ten?>
Print Display Text
1 <? PHP 2 Print "; 3 Print "Hello world!<br>"; 4 Print "I ' m about to learn php!" ; 5 ?>
Print Display Variables
1<?PHP2 $txt 1= "Learn PHP";3 $txt 2= "W3schools.com";4 $x= 5;5 $y= 4;6 7 Print"$txt 1;8 Print"Study PHP at$txt 2<br> ";9 Print $x+$y;Ten?>
6> 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
Example
1 <? php 2 $x = "Hello world!" 3 $y = 5985 4 $z = 10.365 5 $cars = array ("Volvo", "BMW", "Toyota" 6 var _dump () function returns the data type and value 7 var_dump ( $cars );
8 $a = null;
Object Example
1<?PHP2 classCar {3 functionCar () {4 $this->model = "VW";5 }6 }7 8 //Create an Object9 $herbie=NewCar ();Ten One //Show Object Properties A Echo $herbie-model; -?>
PHP Note One (Systax/variables/echo/print/data Type)