Php Tutorial


PHP INTRODUCTION




PHP is a programming language for building dynamic, interactive Web sites. As a general rule, PHP programs run on a Web server, and serve web pages to visitors on request. One of the key features of PHP is that you can embed PHP code within HTML Web pages, making it very easy for you to create dynamic content quickly.
PHP stands for PHP: Hypertext Preprocessor, which means gives you a good idea of its core purpose: to process information and produce hypertext (HTML) as a result.(Developers love recursive acronyms, and PHP . Hypertext Processor is a good example of one.)

Installing PHP

To create and run PHP scripts, you need to have a few things in place:
·         A computer running web server software, such as Apache or Internet Information Server(IIS).
·         The PHP server module installed on the same computer. This module talks to the Web server software; this is the PHP engine that actually does the work of turning your PHP scripts.
·         If you want to build data base driven web-applications-and you probably will – you’ll also need a database server installed.  Options include MySQL, PostgreSQL, and SQL server.  This book mostly refers to using MySQL, so that is the database server that you will install here.

Many combinations of operating system and web server software (not to mention versions of PHP) are available.  For example, operating systems that can run PHP include Linux, windows and Mac OS X, and web server software includes Apache, IIS, and Zeus. 

Installing on Ubuntu Linux :

Linux is a popular choice among PHP web developers, because both technologies are open source.  Furthermore, PHP tends to work well with Linux, Apache, and MySQL database server, In fact, the acronym LAMP (Linux, Apache, My SQL and PHP) is often used to refer to this warning software.
Ubuntu is a popular linux distribution that is easy to install.  You can download it from www.ubuntu.com/getubuntu/download; the desktop edition is fine for developing back PHP applications.  It comes in the form of a CD image, so you can just burn a CD from it, then pop your CD in your computer’s drive and reboot to install it. 
The Ubuntu desktop edition comes with a graphical package manager called Synaptic that you can you use to easily install the Apache web server as well as the PHP module and the MySQL Server one.  To do this follow the step:
1.                   Run Synaptic by choosing System –Administration – Synaptic Package Manager. (You’ll probably be prompted to enter your root (admin) password that you created when you installed Ubuntu).
2.                   Click the reload button in power alone Synaptic’s toolbar to make sure it knows about the latest Ubuntu packages.
3.                   Click the world wide web option in the list of packages groups on the left side of the window than, in the top right window, click the checkbook next to the following:
Packages :apache2,  back php5,  php5-curl, php5-gd, php5-mysql, php5-sqlite, php5-xsl and php-pear.  You will see a pop-up menu appear each time you click below the check box; choose mark for installation from this menu.
Now Click the miscellaneous –text-based option in the packages groups list on the left, then click the checkboxes next to mysql-client and mysql-server.
4.        Often you will see “Mark additional required changes ?” dialog – appear each time you mark one of the packages for installation .  Click the mark button to ensure that Synaptic installs any additional required packages.
5.       Click the apply button in the Synaptic’s toolbar, then in the summary dialog box that appears, click Apply.  Synaptic grabs all the needed packages from the Web and installs them for you.  Along the way you will probably be prompted to enter a password, then inter it again when prompted.  If all goes well you will eventually see a chance you knows applied dialog box appears; click the close button in the dialog box to finish the installation.
6.       At this point, you need to start that Apache web server.  To do this, choose system-administration-services, then click unlock button at the bottom of the services setting dialog box and enter your password.  Now the scroll down to the “web server (apache2)” option, and select its check box to start it.
 That’s it.  You should now have a working Apache web server with PHP and MySQL installed.  Skip to the” testing your installation” select to make sure everything’s working OK.
The packages you have installed you have basic as PHP installation with the functionality needed to follow the contents of this book.  However, you can use synaptic to install extra PHP packages (or remove packages) just easily at any time.
In fact, as of Ubuntu 7.04, then is an even easier way to install Apache, PHP and MySQL is one go. Simply open up a terminal window (applications-accessories-Terminal), then type:
Sudo tasksel install lamp-server
And press Enter.  This installs all the packages needed to have a fully functioning LAMP Web server.  You will be prompted to choose a root password for MySql , during the installation, but apart from that, the process is fully automated.  Again, you will probably need to restart the Web server after installation.

Installing on Windows

PHP one windows can work with Apache or IIS .  For the sake of simplicity, Here is very easy way to install Apache and PHP: WAMP server.  This handy piece of software gives you Apache, my SQL, and PHP all in one handy, easy to install package.
WAMP server comes from the acronym-windows, Apache, MySQL and PHP –which is used to describe any windows, based web server setup that uses these three open source technologies.
To install WAMP Server follow these steps:
1.       Download the latest version of Wamp server from http://www.wampserver.com/en/.
At the time of writing, the latest version was PHP 5.2.6, however, by the time you read this it’s likely that a PHP 5.3 version is available.
Open the WAMP server .EXE file that you downloaded, and followed the instruction on the screen to install the application.
Unblock Apache.  As you run the installer, you may be asked if you want to allow Apache, through the windows firewall, if you want to allow other computer on your network to access the web server, click unblock.  If you are only going to access the web server from a browser on the same computer, you can click keep blocking to improve security.
Enter default mail settings.  During the configuration process you will also be asked to enter a default mail server and e-mail address for PHP to use, you can accept that defaults for now.
Once the setup wizard has completed one, You should see a wampserver icon in your taskbar.
Click the icon to display the wampserver  menu.  Choose the start all services option to fire up the Apache and my SQL servers.
To test that the running server is running correctly, Choose the local host option from the Wampserver menu.  If all has gone according to plan, you should see the icon.  This means that wampserver  was successfully installed. 
Congratulations !
Now you can use.

Installing on Mac OS X

Mac OS X comes with a version of Apache and PHP already installed.  However, it is likely that the installed version is somewhat out of date.  Furthermore, Mac OS X does not come with a MySql package installed by de default; it’s possible to install it. 
Note: You will need MySql  or a similar database system to build database driven web-sites.
As luck would have it, just as windows has WAMP, Mac OS X has MAMP , Mac OS X has MAMP- an all-in-one, easy to install packages that gives you an Apache, MySQL ,and PHP setup on your Mac. The great thing about MAMP (and its windows WAMP equivalents, for that matter)is that it’s self contained.This means that folder; and it’s very easy to uninstall later if you want to.
To install MAMP on your Mac, follow these steps:
1.                   Download the latest MAMP version from www.mamp.info/en/.     ( At the time of writing,two versions are available: MAMP and MAMP PRO. The regular MAMP is fine for the purpose of developing PHP applications on your Mac).
2.                   Open the MAMP .dmg file that you downloaded.
3.                   In the window that pops up, drag the MAMP folder on top of the application folder to install it.
4.                    Open the MAMP folder inside your applications folder in finder, then double-click the MAMP                          icon to launch the application.
5.                   If necessary, click the start server button to start up the Apache and MySQL servers. Once they running, you should green light next to them in the dialog box.
6.                   To test the web server is running correctly, click the open start page button.you now have a working Apache, and MySql installation on your Mac.

Note : By Default, MAMP’s Apache server runs on port 8888, and it MySQL server runs on port 8889.This is to avoid conflicts with any other Apache or MySQL server that might be running on your Mac, but it does mean that you need to specify the Apache port(8888) in the URL in your browser’s address bar.
You can click the preference button in the MAMP application to change the ports that the MAMP Apache and MySQL servers use. For example provided you’ve stopped any other web servers on your Mac that might use the slandered HTTP port 80, you can set the MAMP Apache port of 80 to avoid having to type the port number into your browser’s  address bar.

As with the Linux and Windows install options previously discussed, MAMP install PHP 5.2 at the time of writing, not PHP 5.3 .
Setting your time zone in PHP:
For older versions of PHP , setting the time zone was less important; if you did not specify the time zone, the PHP engine would not attempt to work it out by querying the server it was running on. However, this process was unreliable and is no longer recommended. Therefore, as of PHP 5.3, engine complains with a warning message if you try to use any of the date –related features of PHP without having first configured your time zone.
Fortunately, setting your time zone in PHP is relatively straightforward. To do it, follow these steps:
1.       First look to see if the time zone is already set. Look at the page produced by the testing .php script you just created, and find the date.timezone entry (It will probably be around halfway down the page).If the master value column contains no value or an incorrect timezone, you need to set your timezone, so proceed to Step 2.  However, if the column contents the correct time zone for your server(such as America/Los_Angeles), PHP’s timezone is already set correctly and you can skip the reminder of these steps .
2.       Look for the loaded configuration file entry towards the top of the testing,php page.  This is the path to the shock php.ini file that is used to configure your PHP engine.
3.       Open this file in a text editor, such as notepad(Windows), text edit back(Mac), or text editor(Ubuntu).
Note: you may need root(administrator) access to edit these file.  If you do not have administrator assess, ask your server administrator to set the time zone for you.
4.       Search for the following line in the file:
; date. Timezone =

Note: if for some reason this line is not in your php.ini file, simply add it yourself. 
5.       Remove the semicolon from the start of the line, and add your server’s  timezone after the equals sign at the end of the line.  You can find your timezone in the list at http://www.php.net/ timezone.  For example
Date. Timezone  = America/Los_Angeles
6.       Save the file and quite your text editor.
7.       Restart Apache using the method appropriate for your installation.  For example, on Ubuntu use the system-administrators-service application as described before it.  On windows choose restart all services from the Wampserver icon menu; and on the Mac run the
/applications /MAMP/MAMP/application and click stop servers. 
8.       To test if the setting worked, reload the tasting.php script in your browser and look for the date .timezone entry further up the page.  All Done!

If you cannot(or do not want to) edit your php.ini file, you have other ways to set your time zone:

·         Create an .htaccess file in the document root folder of your Web site(s) and add a directive to the file to set your time zone:
Php_value  date_timezone   America/Los_Angeles

·         Alternatively, towards the start of each PHP script that you create, add a line similar to this:
date_dafault_timezone_set(“America/Los_Angeles” );

PHP Comments-

PHP comments are simply text that is ignored by the PHP engine. The purpose of comments is to let you add message to yourself (and other programmers) that explain what your code does. It’s  always a good idea to add comments to your code, even if you are the only programmer working on it. Sometimes code that makes sense you write it can seem as clear as mud in three months time, so comment can really help.
PHP supports single –line comments and multi-line both. To write a single line comment, start the with either two slashes (//) or a hash symbol (#) .
For Example-
// This code displays the current date
#  This code displays the current date
To write multi-line comments, start the comment with a slash followed by an asterisk (/*) and end the comment with an asterisk followed by a slash (*/), as follows:
/*
This code displays the
 current time in a nice,
easy-to-read format.
*/
So you might comment the PHP code in the hello_with_date.php script like this.
<?
// Get the current date in the readable format
$ current Date = date(“g:i:s  a”);
// Display greeting and Date to the visitor
Echo “Hello, Word! The current time is $currentDate”;
?>

PHP variables:

Variables are fundamental parts of any programming language. A variable is simply a container that holds a certain value. Variables get their names because that certain value can change throughout the execution of the script. It is this ability to certain changing values that make variable so useful.
For example, consider the following simple PHP script:
Echo 4 +2 ;
As you might imagine, this code outputs the number 4 when it is run. This is all well and good; however, if you wanted to print the value of , say, 5+6 instead, you’d have to write another PHP script , as follows:
Echo 7 + 6;
This is where variables come into play .By using variables instead of numbers in your script, you make the script much more useful and flexible:
Echo $x + $y;
You now have a general purpose script. You can set a variable $x and $y to any two values you want, either at some other place in your code, or as result of input from the user. Then, when you run the preceding line of code, the script outputs the some of those two values. Re-run the script with different values for $x and $y, and you get a different result.

Naming Variables-

A variable consist of two parts: the variable’s name and the variable’s value.  Because you will be using variables in your code frequently, it’s best to give your variable’s name you can understand and remember.  Like other programming languages, PHP has certain roles you must follow when naming your variables.
·         Variable names being with a dollar sign ($)
·         The first character after the dollar sign must be a letter or an underscore
·         The remaining characters in the name may be letters, numbers, or underscores without a fixed limit
Variable names are case sensitive ($Variable and $ variable are two distinct variables), so it’s worth sticking to one variable naming method-for example, always using lowercase- to avoid mistakes.  It’s also worth pointing out variable names longer than 30 characters are somewhat impractical.
Here are some examples of PHP variable names:
$my_first_variable
$another variable
$x
$_123

Creating variables

Creating a variable in PHP is known as declaring it.  Declaring a variable is as simple as using its name in your script:
$my_first_variable;
When PHP first sees a variable’s name in a script, it automatically creates the variable at that point. 
Many programming languages prevent you from using a variable without first explicitly declaring it.  But PHP lets you use variables at any point just by naming them.  This is not always the blessing you might think; if you happen to use a non existence variable name by mistake, no error message it generated, and you may end up with hard to find bug.  In most cases, though, it works just fine and is a helpful feature.
When you declare a variable in PHP, it’s a good practice to assign a value to it at the same time.  This is a known as initializing a variable.  By doing this, anyone reading your code knows exactly what value the variable holds at the time it’s created. 
Here is an example of decremented and initializing a variable:
$my_first_variable = 3;
This creates the variable called $my_first_variable, and uses the = operator to assign It the value of 3.  Looking back at the addition example earlier, the following script creates two variables, initializes then with the value 5 and 6 than output their sum(11).
$x =5;
$y =6;
Echo $x + $y;
Data types in PHP
All data stored in PHP variables fall into one of eight basic categories, known as data types.  A variable’s data type determines what operations can be carried out on the variable’s data, as well as the amount of memory needed to hold the data.
PHP supports four scalar data types.  Scalar data means data that contains only a single value.  Here is a list of them, including examples:
Scalar data types
Description
Example
Integer
A whole number
15
float
A Floating-point number
9.03
String
A series of characters
“My name”
Boolean
Represents either true or false
false

As well as the four scalar types, PHP supports two compound types .  Compound data is data that can contain more than one value.  The following table describes PHP’s compound types:
Compound data type
Description
Array
An ordered map(contains name or numbers mapped to values)
Object
A type that may contains properties and method

Finally, PHP supports two special data types, so called because they don’t contain scalar or compound data as such, but have a specific meaning:
Special data Types
Description
Resource
Contains a reference to an external resources, such as a file or database
Null
May only contains null as a value, meaning the variable explicitly does not contain any value

PHP loose Typing

PHP is known as a loosely-typed language.  This means that it is not particularly fussy about the type of data stored in a variable.  It converts a variable’s data type automatically, depending on the on the context in which the variable is used.  For example, you can initialize a variable with an integer value; add a float value to it , thereby turning it into a float; then join it into a string value to produce a longer string. In contract, many other languages, such as java, are strongly-typed; once you set the type of a variable in java, it must always contain data of that type.


PHP’s loose typing is both good and bad. On the plus side, this makes variable very flexible; the same variable can easily be used in different situations.  It also means that you don’t need to worry about specifying the type of a variable when you declare it.  However, PHP won’t tell you if you accidentally pass around data of the wrong type.  For example, PHP will happily let you pass a Floating-point value to a piece of code that expects to be a working on an integer value.  You probably won’t see an error message, but you may discover that the output of your script is not quite what you expected!  These types of errors can be hard to track down.(Fortunately, there is a way to test the type of a variable, as you see in a moment).

Comments

Popular posts from this blog

Rumors of women’s hair cutting in India

President of India