Thursday, 25 October 2012

Loops

"while" loops

Syntax :

while (condition) {
  Statement
 } 
 

 Let's look at a simple example:

<html>
 <head>
 <title>Loops</title>

 </head>
 <body>

 <?php

 $x = 1;
  
 while ($x <= 5) {
    echo "<p>This text is repeated 50 times</p>";
    $x = $x + 1;
 }
 ?>

 </body>

 </html>
 
Output : 

This text is repeated 50 times
This text is repeated 50 times
This text is repeated 50 times
This text is repeated 50 times
This text is repeated 50 times

Time and date functions

Some of the most useful parameters are:
date("y")
Returns the current year from a date - with today's date, it returns: 12
date("m")
Returns the current month from a date - with today's date, it returns: 10
date("n")
Returns the current month from a date without leading zeroes ( eg. "1" instead of "01") - with today's date, it returns: 10
date("F")
Returns the current month name from a date - with today's date, it returns: October
date("d")
Returns the current day of the month from a date - with today's date, it returns: 25
date("l")
Returns the name of the current weekday from a date - with today's date, it returns: Thursday
date("w")
Returns the current day of the week from a date - with today's date, it returns: 4
date("H")
Returns the current hour from a time - with the current time, it returns: 11
date("i")
Returns the current minute from a time - with the current time, it returns: 42
date("s")
Returns the current second from a time - with the current time, it returns: 41
 
sample Program :
 
<html>
   <head>
 <title>time and date</title>
   </head>
 <body>

     <?php  
 
        echo mktime (2,56,0,7,21,1969);

      ?>

 </body>
</html>
 

Example: Now!

Let's try to extend the example by writing both a string and a function - separated by "." (a period) - it's done like this:

<html>
  <head>
     <title>My first PHP document</title>
  </head>
    <body>

 <?php 
 
 echo "<p>Current date and time: " . date("r") . "</p>";

 ?>

    </body>
</html>
 
It will look like this in the browser:
 
 










Your first PHP page

Basically, a PHP file is a text file with the extension .php which consists of:
  • Text
  • HTML tags
  • PHP Scripts
You already know what text and HTML tags are. So let's look a little more at PHP scripts.

PHP Scripts

The goal is that you become accustomed to looking up and finding answers to your questions. PHP is so extensive that you can't to learn all facets in this tutorial. But PHP is not difficult! On the contrary, PHP is often very similar to plain English.
Let's get started with your first PHP page.

Example: Hello World!

Start by making an ordinary HTML document, but name the file page.php and save it in the root of the site:
  • If you use XAMPP, the path for the root is "c:\xampp\htdocs\page.php" on your computer (which is now a server). Read more abot saving PHP files in XAMPP.
  • If you have a website on a host that supports PHP, you simply upload/ftp the file to your web host.
The HTML code should look like this:

<html>
<head>
 <title>My first PHP page</title>

</head>
<body>

</body>
</html>
 
PHP is all about writing commands to a server. So let's write a command to the server. 

 
First, we need to tell the server when the PHP will start and end. In PHP you use the tags <?php and ?> to mark the start and end for the PHP codes that the server must execute (on most servers it will be suficient to use just <? as start tag, but <?php is the most correct to use the first time PHP is used.)
Now try to add the following simple code snippet to your HTML code:

<html>
   <head>
       <title>My first PHP page</title>
   </head>
<body>

 <?php   

 echo "<h1>Hello World!</h1>";

 ?>

</body>
</html>
 
 When we look at the PHP document in a browser, it should look like this:


 









But it gets interesting when you look at the HTML code in the browser (by selecting "view source"):

















PHP Variable Scope

The scope of a variable is the portion of the script in which the variable can be referenced.
PHP has four different variable scopes:
  • local
  • global
  • static
  • parameter

Creating (Declaring) PHP Variables

PHP has no command for declaring a variable.
A variable is created the moment you first assign a value to it:

$myCar="Volvo"; 

After the execution of the statement above, the variable myCar will hold the value Volvo.
Tip: If you want to create a variable without assigning it a value, then you assign it the value of null.
Let's create 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, notice that we did 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.

PHP Variables

Do You Remember Algebra From School?

Do you remember algebra from school? x=5, y=6, z=x+y
Do you remember that a letter (like x) could be used to hold a value (like 5), and that you could use the information above to calculate the value of z to be 11?
These letters are called variables, and variables can be used to hold values (x=5) or expressions (z=x+y).

As with algebra, PHP variables are used to hold values or expressions.
A variable can have a short name, like x, or a more descriptive name, like carName.
Rules for PHP variable names:
  • Variables in PHP starts with a $ sign, followed by the name of the variable
  • The variable name must begin with a letter or the underscore character
  • A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )
  • A variable name should not contain spaces
  • Variable names are case sensitive (y and Y are two different variables)

Comments in PHP

In PHP, we use // to make a one-line comment or /* and */ to make a comment block:

<html>
<body>

<?php
//This is a comment

/*
This is
a comment
block
*/
?>

</body>
</html> 

Basic PHP Syntax

A PHP script always starts with <?php and ends with ?>. A PHP script can be placed anywhere in the document.
On servers with shorthand-support, you can start a PHP script with <? and end with ?>.
For maximum compatibility, we recommend that you use the standard form (<?php) rather than the shorthand form.

<?php

?>

A PHP file must have a .php extension.
A PHP file normally contains HTML tags, and some PHP scripting code.
Below, we have an example of a simple PHP script that sends the text "Hello World" back to the browser:

<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.
In the example above we have used the echo statement to output the text "Hello World".
 

PHP Installation

What do you Need?

If your server supports PHP you don't need to do anything.
Just create some .php files in your web directory, and the server will parse them for you. Because it is free, most web hosts offer PHP support.
However, if your server does not support PHP, you must install PHP.
Here is a link to a good tutorial from PHP.net on how to install PHP5: http://www.php.net/manual/en/install.php

Download PHP

Download PHP for free here: http://www.php.net/downloads.php

Download MySQL Database

Download MySQL for free here: http://www.mysql.com/downloads/

Download Apache Server

Download Apache for free here: http://httpd.apache.org/download.cgi

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

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

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)

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 PHP?

PHP (recursive acronym for PHP: Hypertext Preprocessor) is a widely-used open source general-purpose scripting language that is especially suited for web development and can be embedded into HTML.

Sample Program 1:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <title>Example</title>
    </head>
    <body>

        <?php
            
echo "Hi, I'm a PHP script!";
        
?>
    </body>
</html>