Saturday, 27 August 2011
PHP TUTORIAL VIDEO PAGE 3
15)FUNCTIONS
16)PARAMETRIC FUNCTIONS
17)RETURN VAULUES
18)BEGINING FORMS
19)MORE ON FORMS
20)DATE FUNCTION
21)INCLUDE FUNCTION
Monday, 22 August 2011
PHP BASICS
PHP is a server-side scripting language.
What You Should Already Know
Before you continue you should have a basic understanding of the following:
- HTML/XHTML
- JavaScript
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.)
- PHP is an open source software
- PHP is free to download and use
What is a PHP File?
- PHP files can contain text, HTML tags and scripts
- PHP files are returned to the browser as plain HTML
- PHP files have a file extension of ".php", ".php3", or ".phtml"
What is MySQL?
- MySQL is a database server
- MySQL is ideal for both small and large applications
- MySQL supports standard SQL
- MySQL compiles on a number of platforms
- MySQL is free to download and use
PHP + MySQL
- PHP combined with MySQL are cross-platform (you can develop in Windows and serve on a Unix platform)
Why PHP?
- PHP runs on different platforms (Windows, Linux, Unix, etc.)
- PHP is compatible with almost all servers used today (Apache, IIS, etc.)
- PHP is FREE to download from the official PHP resource: www.php.net
- PHP is easy to learn and runs efficiently on the server side
Where to Start?
To get access to a web server with PHP support, you can:
- Install Apache (or IIS) on your own server, install PHP, and MySQL
- Or find a web hosting plan with PHP and MySQL support
PHP Date() Function
PHP Date() Function
The PHP date() function is used to format a time and/or date.
PHP Date() - Format the Date
The required format parameter in the date() function specifies how to format the date/time.
Here are some characters that can be used:
- d - Represents the day of the month (01 to 31)
- m - Represents a month (01 to 12)
- Y - Represents a year (in four digits)
A list of all the characters that can be used in the format parameter, can be found in our PHP Date reference.
Other characters, like"/", ".", or "-" can also be inserted between the letters to add additional formatting:
<?php
echo date("Y/m/d") . "<br />";
echo date("Y.m.d") . "<br />";
echo date("Y-m-d");
?>
The output of the code above could be something like this:
Output |
2009.05.11
2009-05-11
PHP Functions
PHP Functions
The real power of PHP comes from its functions.
In PHP, there are more than 700 built-in functions.
PHP Built-in Functions
For a complete reference and examples of the built-in functions, please visit our PHP Reference.
PHP Functions
In this chapter we will show you how to create your own functions.
To keep the script from being executed when the page loads, you can put it into a function.
A function will be executed by a call to the function.
You may call a function from anywhere within a page.
Create a PHP Function
A function will be executed by a call to the function.
Syntax |
{
code to be executed;
}
PHP function guidelines:
- Give the function a name that reflects what the function does
- The function name can start with a letter or underscore (not a number)
Example
A simple function that writes my name when it is called:
PHP Code |
{
echo "SamGenius";
}
echo "My name is ";
writeName();
output
My name is Samgenius
PHP Functions - Adding parameters
To add more functionality to a function, we can add parameters. A parameter is just like a variable.
Parameters are specified after the function name, inside the parentheses.
Example 1
The following example will write different first names, but equal last name:
PHP Code |
{
echo $fname . " Refsnes.<br />";
}
echo "My name is ";
writeName("Kai Jim");
echo "My sister name is ";
writeName("Hege");
echo "My brother name is ";
writeName("Stale");
output
My name is Kai Jim Refsnes.
My sister name is Hege Refsnes.
My brother name is Stale Refsnes.
PHP Functions - Return values
To let a function return a value, use the return statement.
Example
<?php
function add($x,$y)
{
$total=$x+$y;
return $total;
}
echo "1 + 16 = " . add(1,16);
?>
PHP Looping - For Loops
PHP Looping - For Loops
Loops execute a block of code a specified number of times, or while a specified condition is true.
The for Loop
The for loop is used when you know in advance how many times the script should run.
Syntax |
{
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)
Note: Each of the parameters above can be empty, or have multiple expressions (separated by commas).
Example
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 Code |
{
echo "The number is " . $i . "
";
}
OUTPUT
The number is 1 The number is 2 The number is 3 The number is 4 The number is 5 |
The foreach Loop
The foreach loop is used to loop through arrays.
Syntax |
{
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) - so on the next loop iteration, you will be looking at the next array value.
Example
The following example demonstrates a loop that will print the values of the given array:
PHP Code |
foreach ($x as $value)
{
echo $value . "
";
}
OUTPUT
one
two
three
try it your self
<?php
for ($i=1; $i<=5; $i++)
{
echo "The number is " . $i . "<br />";
}
//----------www.phptry.com-----------
$x=array("one","two","three");
foreach ($x as $value)
{
echo $value . "<br />";
}
?>
Subscribe to:
Posts (Atom)