> PHP is a popular general-purpose scripting language that is especially suited to web development for making dynamic and interactive Web pages.
>
> Fast, flexible and pragmatic, PHP powers everything from your blog to the most popular websites in the world.
#VSLIDE
* PHP is an acronym for "PHP: Hypertext Preprocessor"
* PHP is a widely-used, open source scripting language
* PHP scripts are executed on the server
* PHP is free to download and use
#VSLIDE
### What is a PHP File?
* PHP files can contain text, HTML, CSS, JavaScript, and PHP code
* PHP code are executed on the server, and the result is returned to the browser as plain HTML
* PHP files have extension `.php`
#VSLIDE
### 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 rights
* PHP can encrypt data
With PHP you are not limited to output HTML. You can output images, PDF files, and even Flash movies. You can also output any text, such as XHTML and XML.
#VSLIDE
### Why PHP?
* PHP runs on various platforms (Windows, Linux, Unix, Mac OS X, etc.)
* PHP is compatible with almost all servers used today (Apache, Nginx, IIS, etc.)
* PHP supports a wide range of databases
* PHP is free. Download it from the official PHP resource: [www.php.net](http://www.php.net)
* PHP is easy to learn and runs efficiently on the server side
#HSLIDE
## Syntax
A PHP script can be placed anywhere in the document.
A PHP script starts with `<?php` and ends with `?>`
#VSLIDE
```php
<?php
// PHP code goes here
?>
```
A PHP file normally contains HTML tags, and some PHP scripting code.
#VSLIDE
```php
<!DOCTYPE html>
<html>
<body>
<h1>My first PHP page</h1>
<?php
echo"Hello World!";
?>
</body>
</html>
```
_**Note:** PHP statements end with a semicolon (`;`)._
#VSLIDE
### Comments
```php
<!DOCTYPE html>
<html>
<body>
<?php
// This is a single-line comment
# This is also a single-line comment
/*
This is a multiple-lines comment block
that spans over multiple
lines
*/
// You can also use comments to leave out parts of a code line
$x=5/* + 15 */+5;
echo$x;
?>
</body>
</html>
```
#VSLIDE
### Case Sensitivity
In PHP, all keywords (e.g. `if`, `else`, `while`, `echo`, etc.), classes, functions, and user-defined functions are **NOT** case-sensitive.
```php
<!DOCTYPE html>
<html>
<body>
<?php
ECHO"Hello World!<br>";
echo"Hello World!<br>";
EcHo"Hello World!<br>";
?>
</body>
</html>
```
#VSLIDE
_**Note:** All variables names **ARE** case-sensitive._
```php
<!DOCTYPE html>
<html>
<body>
<?php
$color="red";
echo"My car is ".$color."<br>";
echo"My house is ".$COLOR."<br>";
echo"My boat is ".$coLOR."<br>";
?>
</body>
</html>
```
#HSLIDE
## Variables
Variables are "containers" for storing information.
#VSLIDE
### Creating (Declaring) PHP Variables
```php
<?php
$txt="Hello world!";
$x=5;
$y=10.5;
?>
```
#VSLIDE
### Rules
* 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)
#VSLIDE
### Typing
PHP is a loosely typed language
PHP automatically converts the variable to the correct data type, depending on its value.
#VSLIDE
### Scope
In PHP, variables can be declared anywhere in the script.
The scope of a variable is the part of the script where the variable can be referenced/used.
PHP has three different variable scopes:
* local
* global
* static
#VSLIDE
### Global and Local Scopes
A variable declared **outside** a function has a **global** scope and can only be accessed outside a function:
```php
<?php
$x=5;// global scope
functionmyTest(){
// using `x` inside this function will generate an error
echo"<p>Variable 'x' inside function is: $x</p>";
}
myTest();
echo"<p>Variable 'x' outside function is: $x</p>";
?>
```
#VSLIDE
### Global and Local Scopes
A variable declared **within** a function has a **local** scope and can only be accessed within that function:
```php
<?php
functionmyTest(){
$x=5;// local scope
echo"<p>Variable 'x' inside function is: $x</p>";
}
myTest();
// using `x` outside the function will generate an error
echo"<p>Variable 'x' outside function is: $x</p>";
?>
```
_**Note:** You can have local variables with the same name in different functions, because local variables are only recognized by the function in which they are declared._
#VSLIDE
### `global` Keyword
The `global` keyword is used to access a global variable from within a function.
To do this, use the `global` keyword before the variables (inside the function).
```php
<?php
$x=5;
$y=10;
functionmyTest(){
global$x,$y;
$y=$x+$y;
}
myTest();
echo$y;// outputs 15
?>
```
#VSLIDE
_**Note:** PHP also stores all global variables in an array called `$GLOBALS[index]`. The `index` holds the name of the variable. This array is also accessible from within functions and can be used to update global variables directly._
```php
<?php
$x=5;
$y=10;
functionmyTest(){
$GLOBALS['y']=$GLOBALS['x']+$GLOBALS['y'];
}
myTest();
echo$y;// outputs 15
?>
```
#VSLIDE
### `static` Keyword
Normally, when a function is completed/executed, all of its variables are deleted.
To prevent variables from being deleted, the `static` keyword can be used.
```php
<?php
functionmyTest(){
static$x=0;
echo$x;
$x++;
}
myTest();
myTest();
myTest();
?>
```
_**Note:** The variable is still local to the function._
#HSLIDE
## Echo / Print
`echo` and `print` are two basic ways to get output in PHP.
#VSLIDE
### Differences
*`echo` has no return value while print has a return value of `1`
*`echo` can take multiple parameters while `print` can take one argument