Guide PHP 4.0 - Home Page

advertisement
Lesson PHP 4.0
Lesson 1 - Introduction to PHP
PHP was created in 1994, and the first publicly available version dates back to 1995 under
the name "Personal Home Page". The language is developed over the years as opensource project, and it is thanks to this feature that many developers are approaching and
are increasingly using this language. PHP is a server-side language, or server-side, in fact,
the code is executed directly from the server, unlike other languages such as JavaScript,
where code is executed on the client. So the PHP language, not only allows you to create
applications, but makes it much easier to manage the dynamic content of a site, even with
the support of some databases, including MySQL, PostgreSQL, Oracle, ODBC.
Lesson 2 - The Tools Needed
In order to work with PHP you need to get some basic tools:
- A publisher of text, Dreamweaver, PSPad, or one of the wide range provided by the Web
- Install an Apache web server (Local Server).
- Install PHP (L 'Script interpreter).
- Installing MySQL (database).
notes:
Alternatively you can use programs that provide ready-made packages that simulate a
Apache web server with MySQL and PHP already included:
EasyPHP, MaxDev o Xaamp
.
Lesson 3 - Local Site
After installing the Apache Web server, you must view the document root ( local folder ).
Some programs, the display immediately after installation, and can be accessed at any
time. Otherwise, open your browser and type in the navigation bar one of the following
addresses:
1° - http://localhost/
2° - http://localhost:80/ ( 80 is the default port for the server configuration )
3° - http://127.0.0.1/
These are the default addresses for local access.
Inside the document root, you can add other folders and create your own site.
Lesson 4 - Creating a Page
Open a blank page from your text editor and save it with the extension .php ( index.php ) in
the folder of your site. To write the page, the tag must be present for opening and closing
of PHP, as shown below:
<?php
?>
notes:
Remember that the browser, read the home page index.php
Lesson 5 - The echo function
As with other languages, PHP also has its own proprietary syntax, so to view the content
of a page you have to make use of the function echo "";
Write in your page code below, and see the result on screen:
<?php
echo"Hello World";
?>
The echo""; function must always be present whenever you want to display a text or the
contents of a variable, as will be explained in subsequent lessons.
Lesson 6 - Variables
PHP variables are characterized by a dollar sign $ followed by the name of the variable
and its content. To view the contents of a variable just use the echo.
Example:
<?php
$number= 10;
echo $number;
$text="Hello World";
echo $text;
?>
Lesson 7 - Arrays
By definition, arrays are objects, whose numerical value or text may be enclosed in a
function (array ('Hello World')) or associated with a variable, using square brackets ([]).
To better understand the concept of objects, as an example, take a box that contains a
pen and a pencil:
<?php
$container = array('Pen','Pencil');
?>
oppure:
<?php
$container[ ] = 'Pen';
$container[ ] = 'Pencil';
?>
The variable container is the box, while the pen and the pencil are the objects contained
within the box.
Lesson 8 - Arrays - Content and Indexes
In the programming language to display the contents of an array, using a sequential index
(0, 1, 2, etc. ...). The index will serve to bring the object to be displayed.
Use the example shown in the previous lesson and see the result on screen:
<?php
$container = array('Pen','Pencil');
echo $container[0];
echo $container[1];
?>
or:
<?php
$container[ ] = 'Pen';
$container[ ] = 'Pencil';
echo $container[0];
echo $container[1];
?>
Lesson 9 - How To Write A Function
By definition, a function is a series of commands or parameters, the programmer sets to
give according to his needs. Before creating the function, you must call it, so the browser
knows that there is a function on the page.
Function call:
myfunction ();
Creation of the function:
function myfunction ()
{
echo "Hello World";
}
We see the correct syntax to write a function:
<?php
myfunction ();
function myfunction ()
{
echo"Hello World";
}
?>
notes:
And 'possible to assign a name to the function of your choice.
Lesson 10 - The Logical Operators
Logical operators are very important, because they allow to assess the contents of a
variable through cycles, or by using the IF function, as will be explained later.
The logical operators are:
>=
<=
==
!=
( greater than or equal )
( less than or equal )
( equal to )
( different from )
&& ( and )
||
( or )
Lesson 11 - The if Function
As mentioned above we can use logical operators to evaluate a variable, and execute at
the same time another action with the function if. Remember that if the function, evaluates
the condition enclosed in parentheses (), and if it is true, returns the action to be
performed, as shown in braces {}. In our case, a message displays on the screen, as in the
example below:
Example logical operator == (equal to):
<php
$number = 10;
if ($number == 10)
{
echo "The variable number is equal to 10";
}
?>
Try changing the value of the variable with a number other than 10, without changing the
condition of the function if. In this case the condition is false, then the message will not be
displayed.
In subsequent lessons, find other examples with logical operators.
Lesson 12 - IF function - Logical Operators
We see other examples with logical operators:
logical operator <= ( less than or equal to ):
<php
$number = 8;
if ($number <= 10)
{
echo "The variable number is less than or equal to 10";
}
?>
or:
<php
$ number = 8;
if ($ number <10)
{
echo "The variable number is less than 10";
}
?>
Lesson 13 - IF function - Logical Operators
We see other examples with logical operators:
logical operator > = ( greater than or equal ):
<php
$number = 12;
if ($number >= 10)
{
echo "The variable number is greater than or equal to 10";
}
?>
or:
<php
$number = 12;
if ($number> 10)
{
echo "The variable number is greater than 10";
}
?>
Lesson 14 - IF function - Logical Operators
We see other examples with logical operators:
logical operator != ( other ):
<php
$number = 15;
if ($number != 10)
{
echo "The variable number is other than 10";
}
?>
logical operator | | ( or ):
Evaluates two conditions simultaneously, if one of them is true, displays the message:
<php
$number = 15;
if ($number <= 10 | | $number == 15)
{
echo "The variable number is equal to 15";
}
?>
To get the false feedback condition, change the value of the variable number, with 11
instead of 15. Do not change the condition in the IF function.
Lesson 15 - IF function - Logical Operators
We see other examples with logical operators:
logical operator & & ( and ), two conditions simultaneously Currency, if all conditions are
true displays the message:
<php
$number = 15;
if ($number> 10 && $number <20)
{
echo "The variable number is greater than 10 and less than 20";
}
?>
If a condition is false, the message is not displayed, as in the example below:
<php
$number = 15;
if ($number == 10 && $number <20)
{
echo "This message does not appear";
}
?>
notes:
For practice, use the examples we've seen and try to change the values of variables, and
conditions to be evaluated.
Lesson 16 - The function if - else
The function if, as mentioned earlier, evaluates one or more conditions, according to the
needs of the programmer. If the condition is met, perform an action if the condition is false
does not do anything. With the function else instead, we can perform an action even if the
condition has not occurred.
example:
<php
$number = 15;
if ($number == 10)
{
echo "The variable number is equal to 10";
}
else {
echo "The variable number is greater or less than 10";
}
?>
Lesson 17 - concatenation (.)
This operator is very useful when we need to combine multiple values in the variables.
example:
<php
$last_name = "Smith";
$name = "John";
/ / Concatenating two variables
$full_name = $last_name. $name;
echo $full_name;
?>
Lesson 18 - The Switch Function
The Switch function, is a control function, functions similar to if / else statements. As we
shall see in the example, the switch function, evaluate the content of a variable, and
performs a certain action.
example:
<php
$number = 20;
switch ($number)
{
case $number == 18:
echo "The variable number is equal to 18";
break;
case number == 19:
echo "The variable number is equal to 19";
break;
case $number == 20:
echo "The variable number is equal to 20";
break;
}
?>
notes:
Use the break statement, the control serves to terminate when the condition has occurred.
In this case the variable number is equal to 20, and displays the appropriate message.
Written and Tested by Daniele Parma
Updated on 08/11/2011
Download