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 be used to control user-access
- PHP can encrypt data
1> Basic PHP Syntax
1
2> Comments in PHP
1
3> Case Sensitivity
In PHP, all keywords (e.g. if, else, while, echo, etc.), classes, functions, and user-defined functions are NOT case-sensitive.
1 ";3 echo "Hello World!";4 EcHo "Hello World!";5 ?>
However; all variable names are case-sensitive.
1 ";4 echo "My house is " . $COLOR . "";5 echo "My boat is " . $coLOR . "";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 are case-sensitive ($age and $AGE are two different variables)
PHP is a Loosely Typed Language
PHP Variables Scope
PHP has three different variable scopes:
- local
- global
- static
PHP also stores all global variables in an array called $GLOBALS[index]. The index holds the name of the variable.
1
5> PHP echo and print Statements
echo and print are more or less the same. They are both used to output data to the screen.
The differences are small:
- echo has no return value while print has a return value of 1 so it can be used in expressions.
- echo can take multiple parameters (although such usage is rare) while print can take one argument.
- echo is marginally faster than print.
echo Display Text
1 PHP is Fun!";3 echo "Hello world!";4 echo "I'm about to learn PHP!";5 echo "This ", "string ", "was ", "made ", "with multiple parameters.";6 ?>
echo Display Variables
1 $txt1"; 8 echo "Study PHP at $txt2"; 9 echo $x + $y;10 ?>
print Display Text
1 PHP is Fun!";3 print "Hello world!";4 print "I'm about to learn PHP!";5 ?>
print Display Variables
1 $txt1"; 8 print "Study PHP at $txt2"; 9 print $x + $y;10 ?>
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
Object Example
1 model = "VW"; 5 } 6 } 7 8 // create an object 9 $herbie = new Car();10 11 // show object properties12 echo $herbie->model;13 ?>