PHP_NOTES1.1
PHP_NOTES1.1
What is PHP?
PHP is a sever side scripting language.
PHP is interpreted and runs on a web server.
PHP differs from other scripting language such as Javascript that runs on the
web browser, since it runs on a web server.
PHP is designed to use alongside HTML, it can be embedded inside HTML or it
can also work with HTML and it returns to the web browser as HTML for end
user viewing.
History of PHP
PHP, known originally as Personal Home Pages, was first conceived in the
autumn of 1994 by Rasmus Lerdorf. After named PHP: Hypertext Preprocessor
PHP as a Language
PHP is a powerful server-side scripting language for creating dynamic and
interactive websites.
PHP is the widely-used, free, and efficient alternative to competitors such as
Microsoft's ASP. PHP is perfectly suited for Web development and can be
embedded directly into the HTML code.
The PHP syntax is very similar to Perl and C. PHP is often used together with
Apache (web server) on various operating systems.
Syntax:
A PHP scripting block always starts with
<?php
and ends with
?>.
A PHP scripting block can be placed anywhere in the document. On servers with
shorthand support enabled you can start a scripting block with
<? and end with ?>.
Example:
<html>
<body>
<?php
echo "Hello World";
?>
</body>
</html>
Each code line in PHP must end with a semicolon. The semicolon is a separator and
is used to distinguish one set of instructions from another.
There are two basic statements to output text with PHP: echo and print.
Comments in PHP
In PHP, we use // to make a single-line comment or /* and */ to make a large
comment block.
Example:
<html>
<body>
<?php
//This is a comment
/* This is a comment block */
?>
</body>
</html>
VARIABLES
A variable starts with the $ sign, followed by the name of the variable
Variable names are case-sensitive ($age and $AGE are two different
variables)
PHP has a total of eight data types which we use to construct our variables
−
Integers − are whole numbers, without a decimal point, like 4195.
Objects − are instances of programmer-defined classes, which can package up both other
Resources − are special variables that hold references to resources external to PHP (such as
database connections).
Example:
<?php
$txt = "Hello World!";
$number = 16;
?>
Strings in PHP
String variables are used for values that contain character strings.
Example:
<?php
$txt="Hello World";
echo $txt;
?>
There is only one string operator in PHP.
Example:
<?php
$txt1="Hello World";
$txt2="1234";
echo $txt1 . " " . $txt2;
?>
Example:
Output:
<?php 12
Echo strlen("Hello world!");
?>
OPERATORS
Arithmetic Operators
Assignment Operators
Logical Operators
Example:
<html>
<body>
<?php $d=date("D");
if ($d=="Fri")
echo "Have a nice weekend!“;
else
echo "Have a nice day!";
?>
</body>
</html>
<html>
<body>
<?php
$d=date("D");
if ($d=="Fri")
{
echo "Hello!<br />";
echo "Have a nice weekend!";
echo "See you on Monday!";
}
?>
</body>
</html>
Example:
<html>
<body>
<?php
$d=date("D");
if ($d=="Fri")
echo "Have a nice weekend!";
elseif ($d=="Sun")
echo "Have a nice Sunday!";
else
echo "Have a nice day!";
?>
</body>
</html>
<html> case 3:
<body> echo "Number 3";
<?php break;
switch ($x) default:
{ echo "No number between 1
case 1: and 3";
echo "Number 1"; }
break; ?>
case 2: </body>
This
echois"Number
how it2";works: </html>
First we have a single expression
break; n (most often a variable), that is evaluated once.
The value of the expression is then compared with the values for each case in the
structure. If there is a match, the block of code associated with that case is
executed. Use break to prevent the code from running into the next case
automatically. The default statement is used if no match is found.
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 lines in a script we can
use loops to perform a task like this.
In PHP, we have the following looping statements:
while - loops through a block of code while a specified condition is true
do...while - loops through a block of code once, and then repeats the loop as
long as a specified condition is true
for - loops through a block of code a specified number of times
foreach - loops through a block of code for each element in an array
while loop
<html>
<body>
<?php
$i=1;
while($i<=5)
{
echo "The number is " . $i . "<br />";
$i++;
}
?>
</body>
</html>
do while loop
<html>
<body>
<?php
$i=1;
do
{
$i++;
echo "The number is " . $i . "<br />";
}
while ($i<=5);
?>
</body>
</html>
for loop
<html>
<body>
<?php
for ($i=1; $i<=5; $i++)
{
echo "The number is " . $i . "<br />";
}
?>
</body>
</html>
foreach loop
<html>
<body>
<?php
$x=array("one","two","three");
foreach ($x as $value)
{
echo $value . "<br />";
}
?>
</body>
</html>
What is an Array?
Numeric Array
A numeric array stores each element with a numeric ID key.
Example #1:
In this example the ID key is automatically assigned:
$names = array("Peter","Quagmire","Joe");
Example #2:
In this example we assign the ID key manually:
$names[0] = "Peter";
$names[1] = "Quagmire";
$names[2] = "Joe";
Associative Arrays
An associative array, each ID key is associated with a value.
When storing data about specific named values, a numerical array is not always
the best way to do it. With associative arrays we can use the values as keys and
assign values to them.
Example #1:
In this example we use an array to assign ages to the different persons:
$ages = array("Peter"=>32, "Quagmire"=>30, "Joe"=>34);
Example #2:
This example is the same as example 1, but shows a different way of creating the
array:
$ages['Peter'] = "32";
$ages['Quagmire'] = "30";
$ages['Joe'] = "34";
FUNCTIONS IN PHP
A function is a block of code that can be executed whenever we need it.
Example:
<html>
<body>
<?php
functionwriteMyName()
{
echo “Juan de la Cruz";
}
writeMyName();
?>
</body>
</html>
Example #1- The following example will write different first names, but the
same last name:
<html>
<body>
<?php
Function writeMyName($fname)
{
echo $fname . " Cuneta.<br />";
}
The example below contains an HTML form with two input fields and a submit
button:
<html>
<body>
<form action="welcome.php" method="post">
Name: <input type="text" name="fname" />
Age: <input type="text" name="age" />
<input type="submit" />
</form>
</body>
</html>
When a user fills out the form above and click on the submit button, the
form data is sent to a PHP file, called "welcome.php":
Form Validation:
a. User input should be validated on the browser whenever possible (by client
scripts). Browser validation is faster and reduces the server load.
b. You should consider server validation if the user input will be inserted into a
database. A good way to validate a form on the server is to post the form to
itself, instead of jumping to a different page. The user will then get the error
messages on the same page as the form. This makes it easier to discover the
error.
PHP $_POST
In PHP, the predefined $_POST variable is used to collect values in a form with
method="post".
Example:
<form action="welcome.php" method="post">
Name: <input type="text" name="fname" />
Age: <input type="text" name="age" />
<input type="submit" />
</form>
When the user clicks the "Submit" button, the URL will look like this:
http://www.w3schools.com/welcome.php
The "welcome.php" file can now use the $_POST variable to collect form data
(the names of the form fields will automatically be the keys in the $_POST
array):
However, because the variables are not displayed in the URL, it is not possible to
bookmark the page.
PHP $_GET
In PHP, the predefined $_GET variable is used to collect values in a form with
method="get".
Example:
<form action="welcome.php" method="get">
Name: <input type="text" name="fname" />
Age: <input type="text" name="age" />
<input type="submit" />
</form>
When the user clicks the "Submit" button, the URL sent to the server could
look something like this:
http://www.w3schools.com/welcome.php?fname=Peter&age=37
The "welcome.php" file can now use the $_GET variable to collect form data
(the names of the form fields will automatically be the keys in the $_GET
array):
Welcome <?php echo $_GET["fname"]; ?>.<br />
You are <?php echo $_GET["age"]; ?> years old!
Note:
a. This method should not be used when sending passwords or other sensitive
information!However, because the variables are displayed in the URL, it is
possible to bookmark the page. This can be useful in some cases.
b. The get method is not suitable for very large variable values. It should not be
used with values exceeding 2000 characters.
Example:
Welcome <?php echo $_REQUEST["fname"]; ?>!<br />
You are <?php echo $_REQUEST["age"]; ?> years old.