PHP is a server-side scripting language.PHP is a powerful tool for making dynamic and interactive Web pages.
PHP is the widely-used, free, and efficient alternative to competitors such as Microsoft's ASP.
Before you continue you should have a basic understanding of the following:
PHP stands for PHP: Hypertext Preprocessor. PHP is an open source software, PHP files have a file extension of ".php", ".php3", or ".phtml"
PHP supports many databases (MySQL, Informix, Oracle, Sybase, Solid, PostgreSQL, Generic ODBC, etc.)
PHP combined with MySQL are cross-platform (you can develop in Windows and serve on a Unix platform).
-
PHP runs on different platforms (Windows, Linux, Unix, etc.)
-
PHP is compatible with almost all servers used today (Apache, IIS, etc.)
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 ?>.
For maximum compatibility, we recommend that you use the standard form (<?php) rather than the shorthand form.
<?php ?>
we have an example of a simple PHP script which sends the text "Hello World" to the browser:
<html>
<body>
<?php
echo "Hello World";
?>
</body>
</html>
All variables in PHP start with a $ sign symbol. The correct way of declaring a variable in PHP:
$var_name = value;
New PHP programmers often forget the $ sign at the beginning of the variable. In that case it will not work.
Let's try creating a variable containing a string, and a variable containing a number:
<?php
$txt="Hello World!";
$x=16;
?>
PHP is a Loosely Typed Language
In PHP, a variable does not need to be declared before adding a value to it.In the example above, you see that you do not have to tell PHP which data type the variable is.PHP automatically converts the variable to the correct data type, depending on its value.In a strongly typed programming language, you have to declare (define) the type and name of the variable before using it.In PHP, the variable is declared automatically when you use it.
A variable name should not contain spaces. If a variable name is more than one word, it should be separated with an underscore ($my_string), or with capitalization ($myString)
The Concatenation Operator
There is only one string operator in PHP.
The concatenation operator (.) is used to put two string values together.To concatenate two string variables together, use the concatenation operator:
<?php
$txt1="Hello World!";
$txt2="What a nice day!";
echo $txt1 . " " . $txt2;
?>
The strlen() function is used to return the length of a string.
<?php
echo strlen("Hello world!");
?>
The strpos() function is used to search for character within a string.If a match is found, this function will return the position of the first match. If no match is found, it will return FALSE.
<?php
echo strpos("Hello world!","world");
?>
You can use conditional statements in your code to do this.
In PHP we have the following conditional statements:
-
if statement - use this statement to execute some code only if a specified condition is true
-
if...else statement - use this statement to execute some code if a condition is true and another code if the condition is false
-
if...elseif....else statement - use this statement to select one of several blocks of code to be executed
-
switch statement - use this statement to select one of many blocks of code to be executed
Use the if statement to execute some code only if a specified condition is true.
<?php
$d=date("D");
if ($d=="Fri") echo "Have a nice weekend!";
?>
Use the if....else statement to execute some code if a condition is true and another code if a condition is false.
<?php
$d=date("D");
if ($d=="Fri")
echo "Have a nice weekend!";
else
echo "Have a nice day!";
?>
Use the if....elseif...else statement to select one of several blocks of code to be executed.
<?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!";
?>
This is how it works: First we have a single expression 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
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";
}
?>
An array stores multiple values in one single variable. A variable is a storage area holding a number or text. The problem is, a variable will hold only one value.
An array is a special variable, which can store multiple values in one single variable.If you have a list of items (a list of car names, for example), storing the cars in single variables could look like this:
In PHP, there are three kind of arrays:
In the following example the index are automatically assigned (the index starts at 0):
$cars=array("Saab","Volvo","BMW","Toyota");
In the following example we assign the index manually:
$cars[0]="Saab";
$cars[1]="Volvo";
$cars[2]="BMW";
$cars[3]="Toyota";
In the following example you access the variable values by referring to the array name and index:
<?php
$cars[0]="Saab";
$cars[1]="Volvo";
$cars[2]="BMW";
$cars[3]="Toyota";
echo $cars[0] . " and " . $cars[1] . " are Swedish cars.";
?>
An associative array, each ID key is associated with a value. With associative arrays we can use the values as keys and assign values to them.
In this example we use an array to assign ages to the different persons:
$ages = array("Peter"=>32, "Quagmire"=>30, "Joe"=>34);
This example is the same as example 1, but shows a different way of creating the array:
<?php
$ages['Peter'] = "32";
$ages['Quagmire'] = "30";
$ages['Joe'] = "34";
echo "Peter is " . $ages['Peter'] . " years old.";
?>
PHP Loops
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
The while loop executes a block of code while a condition is true.
The example below defines a loop that starts with i=1. The loop will continue to run as long as i is less than, or equal to 5. i will increase by 1 each time the loop runs:
<?php
$i=1;
while($i<=5)
{
echo "The number is " . $i . "<br />";
$i++;
}
?>
The do...while statement will always execute the block of code once, it will then check the condition, and repeat the loop while the condition is true. The example below defines a loop that starts with i=1. It will then increment i with 1, and write some output. Then the condition is checked, and the loop will continue to run as long as i is less than, or equal to 5:
<?php
$i=1;
do
{
$i++;
echo "The number is " . $i . "<br />";
}
while ($i<=5);
?>
The for loop is used when you know in advance how many times the script should run.
for (init; condition; increment)
{
code to be executed;
}
Parameters:
-
init: Mostly used to set a counter (but can be any code to be executed once at the beginning of the loop)
-
condition: Evaluated for each loop iteration. If it evaluates to TRUE, the loop continues. If it evaluates to FALSE, the loop ends.
-
increment: Mostly used to increment a counter (but can be any code to be executed at the end of the loop)
<?php
for ($i=1; $i<=5; $i++)
{
echo "The number is " . $i . "<br />";
}
?>
The foreach loop is used to loop through arrays. For every loop iteration, the value of the current array element is assigned to $value (and the array pointer is moved by one) - so on the next loop iteration, you'll be looking at the next array value.
<?php
$x=array("one","two","three");
foreach ($x as $value)
{
echo $value . "<br />";
}
?>
Output:
one
two
three
The real power of PHP comes from its functions.In PHP, there are more than 700 built-in functions.
In this chapter we will show you how to create your own functions.To keep the browser from executing a script when the page loads, you can put your script into a function.A function will be executed by a call to the function.You may call a function from anywhere within a page.
<?php
function writeName()
{
echo "Kai Jim Refsnes";
}
echo "My name is ";
writeName();
?>
To add more functionality to a function, we can add parameters. A parameter is just like a variable.
<?php
function writeName($fname)
{
echo $fname . " Refsnes.<br />";
}
echo "My name is ";
writeName("Kai Jim");
echo "My sister's name is ";
writeName("Hege");
echo "My brother's name is ";
writeName("Stale");
?>
<?php
function writeName($fname,$punctuation)
{
echo $fname . " Refsnes" . $punctuation . "<br />";
}
echo "My name is ";
writeName("Kai Jim",".");
echo "My sister's name is ";
writeName("Hege","!");
echo "My brother's name is ";
writeName("Ståle","?");
?>
The rest of php basic is detailed on next tutorials, watch it.