HTML Basic
HTML Basic
MODULE 1:
What is PHP?
A PHP file contains PHP tags and ends with the extension ".php".
PHP means - Personal Home Page, but it now stands for the
recursive backronym PHP: Hypertext Preprocessor.
Php Syntax
What is PHP?
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
Why PHP?
PHP runs on various platforms (Windows, Linux, Unix, Mac OS X,
etc.)
PHP is compatible with almost all servers used today (Apache, IIS,
etc.)
PHP supports a wide range of databases
PHP is free. Download it from the official PHP
resource: www.php.net
PHP is easy to learn and runs efficiently on the server side
PHP Syntax
A PHP script is executed on the server, and the plain HTML result is
sent back to the browser.
Example
<!DOCTYPE html>
<html>
<body>
<?php
echo "Hello World!";
?>
</body>
</html>
Note: PHP statements end with a semicolon (;).
Comments in PHP
A comment in PHP code is a line that is not read/executed as part of the
program. Its only purpose is to be read by someone who is looking at the
code.
Comments can be used to:
Let others understand what you are doing
Remind yourself of what you did - Most programmers have
experienced coming back to their own work a year or two later and
having to re-figure out what they did. Comments can remind you of
what you were thinking when you wrote the code
PHP supports several ways of commenting:
Example
<!DOCTYPE html>
<html>
<body>
<?php
// This is 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>
PHP Case Sensitivity
In PHP, all keywords (e.g. if, else, while, echo, etc.), classes, functions,
and user-defined functions are NOT case-sensitive.
In the example below, all three echo statements below are legal (and
equal):
Example
<!DOCTYPE html>
<html>
<body>
<?php
ECHO "Hello World!<br>";
echo "Hello World!<br>";
EcHo "Hello World!<br>";
?>
</body>
</html>
However; all variable names are case-sensitive.
In the example below, only the first statement will display the value of
the $color variable (this is because $color, $COLOR, and $coLOR are
treated as three different variables):
Example
<!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>
PHP Variables
Variables are "containers" for storing information.
<?php
$txt = "Hello world!";
$x = 5;
$y = 10.5;
echo $txt;
echo "<br>";
echo $x;
echo "<br>";
echo $y;
?>
</body>
</html>
After the execution of the statements above, the variable $txt will hold
the value Hello world!, the variable $x will hold the value 5, and the
variable $y will hold the value 10.5.
Note: When you assign a text value to a variable, put quotes around the
value.
Note: Unlike other programming languages, PHP has no command for
declaring a variable. It is created the moment you first assign a value to
it.
PHP Variables
A variable can have a short name (like x and y) or a more descriptive
name (age, carname, total_volume).
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)
Output Variables
The PHP echo statement is often used to output data to the screen.
The following example will show how to output text and a variable:
Example
<!DOCTYPE html>
<html>
<body>
<?php
$txt = "St.Francis College";
echo "I love $txt!";
?>
</body>
</html>
The following example will produce the same output as the example
above:
Example
<?php
$txt = " St.Francis College ";
echo "I love " . $txt . "!";
?>
The following example will output the sum of two variables:
<?php
$x = 5;
$y = 4;
echo $x + $y;
?>
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>";
?>
A variable declared within a function has a LOCAL SCOPE and can
only be accessed within that function:
Example
<?php
function myTest() {
$x = 5; // local scope
echo "<p>Variable x inside function is: $x</p>";
}
myTest();
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.
function myTest() {
global $x, $y;
$y = $x + $y;
}
myTest();
echo $y; // outputs 15
?>
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.
The example above can be rewritten like this:
Example
<?php
$x = 5;
$y = 10;
function myTest() {
$GLOBALS['y'] = $GLOBALS['x'] + $GLOBALS['y'];
}
myTest();
echo $y; // outputs 15
?>
myTest();
myTest();
myTest();
?>
Then, each time the function is called, that variable will still have the
information it contained from the last time the function was called.
Note: The variable is still local to the function.
In PHP there are two basic ways to get output: echo and print.
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.
Display Variables
The following example shows how to output text and variables with the
echo statement:
Example
<?php
$txt1 = "Learn PHP";
$txt2 = "St.Francis";
$x = 5;
$y = 4;
echo "<h2>$txt1</h2>";
echo "Study PHP at $txt2<br>";
echo $x + $y;
?>
Display Variables
The following example shows how to output text and variables with the
print statement:
Example
<?php
$txt1 = "Learn PHP";
$txt2 = "St.Francis";
$x = 5;
$y = 4;
print "<h2>$txt1</h2>";
print "Study PHP at $txt2<br>";
print $x + $y;
?>
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
PHP String
A string is a sequence of characters, like "Hello world!".
A string can be any text inside quotes. You can use single or double
quotes:
Example
<?php
$x = "Hello world!";
$y = 'Hello world!';
echo $x;
echo "<br>";
echo $y;
?>
PHP Integer
An integer data type is a non-decimal number between -2,147,483,648
and 2,147,483,647.
Rules for integers:
An integer must have at least one digit
An integer must not have a decimal point
An integer can be either positive or negative
Integers can be specified in three formats: decimal (10-based),
hexadecimal (16-based - prefixed with 0x) or octal (8-based -
prefixed with 0)
In the following example $x is an integer. The PHP var_dump() function
returns the data type and value:
Example
<?php
$x = 5985;
var_dump($x);
?>
PHP Float
A float (floating point number) is a number with a decimal point or a
number in exponential form.
In the following example $x is a float. The PHP var_dump() function
returns the data type and value:
Example
<?php
$x = 10.365;
var_dump($x);
?>
PHP Boolean
A Boolean represents two possible states: TRUE or FALSE.
$x = true;
$y = false;
Booleans are often used in conditional testing. You will learn more about
conditional testing in a later chapter of this tutorial.
PHP Array
An array stores multiple values in one single variable.
In the following example $cars is an array. The PHP var_dump() function
returns the data type and value:
Example
<?php
$cars = array("Volvo","BMW","Toyota");
var_dump($cars);
?>
PHP Object
An object is a data type which stores data and information on how to
process that data.
In PHP, an object must be explicitly declared.
First we must declare a class of object. For this, we use the class
keyword. A class is a structure that can contain properties and methods:
Example
<?php
class Car {
function Car() {
$this->model = "VW";
}
}
// create an object
$herbie = new Car();
PHP Resource
The special resource type is not an actual data type. It is the storing of a
reference to functions and resources external to PHP.
A common example of using the resource data type is a database call.
PHP 5 Strings
A string is a sequence of characters, like "Hello world!".
Reverse a String
The PHP strrev() function reverses a string:
Example
<?php
echo strrev("Hello world!"); // outputs !dlrow olleH
?>
The output of the code above will be: !dlrow olleH.
PHP CONSTANTS
Constants are like variables except that once they are defined they
cannot be changed or undefined.
PHP CONSTANTS
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.
function myTest() {
echo GREETING;
}
myTest();
?>
PHP OPERATORS
Operators are used to perform operations on variables and values.
PHP divides the operators in the following groups:
Arithmetic operators
Assignment operators
Comparison operators
Increment/Decrement operators
Logical operators
String operators
Array operators
% Modulus $x % $y Remainder of $x
divided by $y
x += y x=x+y Addition
x -= y x=x-y Subtraction
x *= y x=x*y Multiplication
x /= y x=x/y Division
x %= y x=x%y Modulus
or Or $x or $y True if either $x or $y
is true
|| Or $x || $y True if either $x or $y
is true
different conditions.
Very often when you write code, you want to perform different actions for
different conditions. You can use conditional statements in your code to
do this.
The example below will output "Have a good day!" if the current time
(HOUR) is less than 20:
Example
<?php
$t = date("H");
?>
Syntax
if (condition) {
code to be executed if condition is true;
} else {
code to be executed if condition is false;
}
The example below will output "Have a good day!" if the current time is
less than 20, and "Have a good night!" otherwise:
Example
<?php
$t = date("H");
} else {
?>
Syntax
if (condition) {
} elseif (condition) {
} else {
}
The example below will output "Have a good morning!" if the current time
is less than 10, and "Have a good day!" if the current time is less than
20. Otherwise it will output "Have a good night!":
Example
<?php
$t = date("H");
} else {
?>
Syntax
switch (n) {
case label1:
case label2:
break;
case label3:
break;
...
default:
labels;
Example
<?php
$favcolor = "red";
switch ($favcolor) {
case "red":
echo "Your favorite color is red!";
break;
case "blue":
break;
case "green":
break;
default:
?>
6 PHP LOOPS
Often when you write code, you want the same block of code to run over
and over again in a row. Instead of adding several almost equal code-
lines in a script, we can use loops to perform a task like this.
Syntax
while (condition is true) {
code to be executed;
The example below first sets a variable $x to 1 ($x = 1). Then, the while
loop will continue to run as long as $x is less than, or equal to 5 ($x <=
5). $x will increase by 1 each time the loop runs ($x++):
Example
<?php
$x = 1;
while($x <= 5) {
$x++;
?>
THE PHP DO...WHILE LOOP
The do...while loop will always execute the block of code once, it will
then check the condition, and repeat the loop while the specified
condition is true.
Syntax
do {
code to be executed;
The example below first sets a variable $x to 1 ($x = 1). Then, the do
while loop will write some output, and then increment the variable $x
with 1. Then the condition is checked (is $x less than, or equal to 5?),
and the loop will continue to run as long as $x is less than, or equal to 5:
Example
<?php
$x = 1;
do {
$x++;
?>
Example
<?php
$x = 6;
do {
$x++;
?>
The for loop is used when you know in advance how many times the
script should run.
Syntax
for (init counter; test counter; increment counter)
code to be executed;
Parameters:
Example
<?php
?>
The foreach loop works only on arrays, and is used to loop through each
key/value pair in an array.
Syntax
foreach ($array as $value) {
code to be executed;
For every loop iteration, the value of the current array element is
assigned to $value and the array pointer is moved by one, until it
reaches the last array element.
The following example demonstrates a loop that will output the values of
the given array ($colors):
Example
<?php
?>
name1=value1&name2=value2&name3=value3
Spaces are removed and replaced with the + character and any
other nonalphanumeric characters are replaced with a hexadecimal
values. After the information is encoded it is sent to the server.
The GET Method
The GET method sends the encoded user information appended
to the page request. The page and the encoded information are
separated by the ? character.
http://www.test.com/index.htm?name1=value1&name2=value2
The GET method produces a long string that appears in your
server logs, in the browser's Location: box.
The GET method is restricted to send upto 1024 characters only.
Never use GET method if you have password or other sensitive
information to be sent to the server.
GET can't be used to send binary data, like images or word
documents, to the server.
The data sent by GET method can be accessed using
QUERY_STRING environment variable.
The PHP provides $_GET associative array to access all the sent
information using GET method.
We can create and use forms in PHP. To get form data, we need to
use PHP superglobals $_GET and $_POST.
The form request may be get or post. To retrieve data from get
request, we need to use $_GET, for post request $_POST.
<?php
$_GET['variable_name'];
?>
HERE,
EXAMPLE 1
<html>
<body>
<input type="submit">
</form>
</body>
</html>
When the user clicks on the "Submit button", the URL will be something
like this:
registration.php looks like this:
<html>
<body>
</body>
</html>
Example 2:
File: form1.html
<HTML>
<BODY>
<form action="welcome.php" method="get">
Name: <input type="text" name="name"/>
<input type="submit" value="visit"/>
</form>
</BODY>
</HTML>
File: welcome.php
<?php
$name=$_GET["name"];//receiving name field value in $name variable
echo "Welcome, $name";
?>
PHP Post Form
Post request is widely used to submit form that have large amount of
data such as file upload, image upload, login form, registration form
etc.
The data passed through post request is not visible on the URL
browser so it is secured. You can send large amount of data through
post request.It has the following syntax.
<?php
$_POST['variable_name'];
?>
HERE,
EXAMPLE 1
<html>
<body>
<input type="submit">
</form>
</body>
</html>
When the user clicks on the "Submit button", the URL will be something
like this:
Registration.php looks like this:
<html>
<body>
</body></html>
Example 2:
File: form1.html
<HTML>
<BODY>
<form action="login.php" method="post">
Name:<input type="text" name="name"><BR>
Password:<input type="password" name="password">
<Br>
<input type="submit" value="login"><BR>
</form>
</BODY>
</HTML>
File: login.php
<?php
$name=$_POST["name"];//receiving name field value in $name variable