Escolar Documentos
Profissional Documentos
Cultura Documentos
Internet Programming II
What is PHP?
PHP stands for PHP: Hypertext Preprocessor
PHP is a server-side scripting language, like ASP
PHP scripts are executed on the server
PHP supports many databases (MySQL, Informix,
Oracle, Sybase, Solid, PostgreSQL, Generic ODBC,
etc.)
Use xamp or wamp which comprises apache server,
PHP and MySQL.
What is a PHP File?
PHP files may contain text, HTML, CSS,
JavaScript, and PHP code
PHP files are returned to the browser as plain
HTML
PHP files have a file extension of ".php
In PHP, all keywords (e.g. if, else, while, echo, etc.),
classes, functions, and user-defined functions are
NOT case-sensitive.
Basic PHP Syntax
A PHP scripting block always starts with <?php and
ends with ?>
<!DOCTYPE html>
<html>
<body>
<h1>My first PHP page</h1>
<?php
echo "Hello World!";
?>
</body>
</html>
Get Started
1. Install wamp 2.1
2. Run wamp 2.1
3. Wait until the color of wamp icon on the taskbar
changed to GREEN
4. Save your php file by an extention .php(eg. Home.php)
on c:\wamp\www\
5. To run your file, open any browser by writing the
following path on the URL: localhost/home.php
6. Then, load the page by pressing ENTER key
Comments in 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 */
// comments to leave out parts of a code line
$x = 5 /* + 15 */ + 5;
echo $x;
?>
</body></html>
Variables in PHP
Variables are "containers" for storing information.
all variable names are case-sensitive.
In PHP, a variable starts with the $ sign, followed by the
name of the variable:
$var_name = value;
<?php
$txt = "Hello World!";
$number = 16;
?>
In PHP the variable is declared automatically when you
use it.
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
PHP Strings
<?php
$txt1="Hello our";
$txt2='World';
echo $txt1 . " " . $txt2;
?>
PHP String Functions
<?php
?>
PHP Constants
Constants are like variables except that once they
are defined they cannot be changed or undefined.
A constant is an identifier (name) for a simple
value. The value cannot be changed during the
script.
A valid constant name starts with a letter or
underscore (no $ sign before the constant name).
Note: Unlike variables, constants are
automatically global across the entire script.
Syntax
define(name, value, case-insensitive)
Examples
<?php
define("GREETING", "Welcome to W3Schools.com!",
true);
echo greeting;
?>
<?php
define("GREETING", "Welcome to W3Schools.com!");
function myTest() {
echo GREETING;
}
myTest();
?>
PHP Arithmetic Operators
The PHP arithmetic operators are used with numeric
values to perform common arithmetical operations, such
as addition, subtraction, multiplication etc.
Operator Name Example Result
+ Addition $x + $y Sum of $x and $y
- Subtraction $x - $y Difference of $x and $y
* Multiplication $x * $y Product of $x and $y
/ Division $x / $y Quotient of $x and $y
Remainder of $x divided by
% Modulus $x % $y
$y
Result of raising $x to the
** Exponentiation $x ** $y $y'th power (Introduced in
Example:
<!DOCTYPE html>
<html>
<body>
<?php
$num1 = 10;
$num2 = 80;
$num3 = 20;
echo $sum;
?>
</body>
</html>
PHP Assignment Operators
The PHP assignment operators are used with numeric values to
write a value to a variable.
The basic assignment operator in PHP is "=". It means that the
left operand gets set to the value of the assignment expression
on the right.
Assignment Same as... Description
The left operand gets set to the value
x=y x=y
of the expression on the right
x += y x=x+y Addition
x -= y x=x-y Subtraction
x *= y x=x*y Multiplication
x /= y x=x/y Division
Example:
<!DOCTYPE html>
<html>
<body>
<?php
$x = 10;
$x /= 5;
echo $x;
?>
</body>
</html>
PHP Comparison Operators
The PHP comparison operators are used to compare two
values (number or string):
Operator Name Example Result
== Equal $x == $y Returns true if $x is equal to $y
Returns true if $x is equal to $y, and
=== Identical $x === $y
they are of the same type
!= Not equal $x != $y Returns true if $x is not equal to $y
<> Not equal $x <> $y Returns true if $x is not equal to $y
Returns true if $x is not equal to $y,
!== Not identical $x !== $y
or they are not of the same type
> Greater than $x > $y Returns true if $x is greater than $y
< Less than $x < $y Returns true if $x is less than $y
Greater than or Returns true if $x is greater than or
>= $x >= $y
equal to equal to $y
Example:
<!DOCTYPE html>
<html>
<body>
<?php
$num='4';
$num2=4;
if($num===$num2)
echo 'Equal';
else
echo 'Inequal';
?>
</body>
PHP Increment / Decrement Operators
<?php
$x = 10;
echo ++$x;
?>
</body>
</html>
PHP Logical Operators
The PHP logical operators are used to combine
conditional statements.
<?php
$num = 100;
$lower = 50;
$upper = 1000;
</body>
</html>
PHP String Operators
PHP has two operators that are specially designed
for strings.
<?php
$txt1 = "Hello";
$txt1 .= " world!";
echo $txt1;
?>
</body>
</html>
PHP Array Operators
The PHP array operators are used to compare arrays
Operator Name Example Result
+ Union $x + $y Union of $x and $y
Returns true if $x and $y have the
== Equality $x == $y
same key/value pairs
Returns true if $x and $y have the
=== Identity $x === $y same key/value pairs in the same
order and of the same types
Returns true if $x is not equal to
!= Inequality $x != $y
$y
Returns true if $x is not equal to
<> Inequality $x <> $y
$y
The If...Else Statement
<html>
<body>
<?php
$d=date("D");
if ($d=="Fri") {
echo "Hello!<br />";
echo "Have a nice weekend!";
echo "See you on Monday!";
} //else if ($d=="Sun") echo "Have a nice Sunday!";
else
echo "Have a nice day!";
?>
</body>
</html>
The Switch Statement
<html>
<body>
<?php
$x=4;
switch ($x) {
case 1: echo "Number 1"; break;
case 2: echo "Number 2"; break;
case 3: echo "Number 3"; break;
default: echo "No number between 1 and 3";
}
?>
</body>
</html>
The while Loop
<?php
$x = 1;
while($x <= 5) {
echo "The number is: $x <br>";
$x++;
}
?>
The do...while Loop
In a do while loop the condition is tested AFTER executing the
statements within the loop. This means that the do while loop
would execute its statements at least once, even if the condition
is false the first time.
<?php
$x = 6;
do {
echo "The number is: $x <br>";
$x++;
} while ($x<=5);
?>
The for Loop
<?php
?>
The PHP foreach Loop
The foreach loop works only on arrays, and is used to
loop through each key/value pair in an array.
foreach ($array as $value) {
code to be executed; }
<?php
$colors = array("red", "green", "blue", "yellow");
foreach ($colors as $val) {
echo "$val <br>";}
?>
die and exit Functions
<?php
echo 'Hello';
?>
PHP Functions
A function name can start with a letter or underscore
(not a number).
Function names are NOT case-sensitive.
<?php
function writeMsg() {
echo " world Hello!";
}
writeMsg(); // call the function
?>
PHP Function Arguments
Information can be passed to functions through arguments. An
argument is just like a variable.
Arguments are specified after the function name, inside the
parentheses. You can add as many arguments as you want, just
separate them with a comma.
<?php
function familyName($fname) {
echo "$fname David.<br>";
}
familyName("Jani");
familyName("Hege");
familyName("Stale");
?>
PHP Default Argument Value
<?php
function setHeight($minheight = 50) {
echo "The height is : $minheight <br>";
}
setHeight(350);
setHeight(); // will use the default value of 50
setHeight(135);
setHeight(80);
?>
PHP Functions - Returning values
<?php
function sum($x, $y) {
$z = $x + $y;
return $z;
}
?>
Associative Arrays
With associative arrays we can use the values as keys and
assign values to them
<?php
$ages = array("Peter"=>32, "Quagmire"=>30, "Joe"=>34);
$ages['Peter'] = "32";
$ages['Quagmire'] = "30";
$ages['Joe'] = "34";
?>
Example:
<?php
$age = array("Peter"=>"35", "Ben"=>"37",
"Joe"=>"43");
arsort($age);
foreach($age as $x => $x_val) {
echo "Key=" . $x . ", x_value=" . $x_val;
echo "<br>";
}
?>
PHP Variables Scope
PHP has three different variable scopes: Local, Global, and static
Global Scope
A variable declared outside a function has a GLOBAL SCOPE
and can only be accessed outside a function:
<?php
$x = 5; // global scope
function myTest() {
// 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>";
Local Scope
A variable declared within a function has a LOCAL SCOPE and
can only be accessed within that function:
<?php
function myTest() {
$x = 5; // local scope
echo "<p>Variable x inside function is: $x</p>";
}
myTest();
<html><body>
Welcome <?php echo $_GET["name"]; ?><br>
Your email address is: <?php echo $_GET["email"];?>
</body></html>
Example
<!DOCTYPE HTML>
<html><body>
<?php
$match = 123;
if (isset($_POST['pass'])) {
$psw = $_POST['pass'];
if (!empty($psw)) {
if ($psw==$match){
echo 'correct password';
} else {
echo 'incorrect password';
}
} else {
echo 'enter your password';
}
}
?>
<form action = "cs.php" method = "post">
Password: <input type = "password" name = "pass">
<input type = "submit" value = "submit">
</form>
</body></html>