PDA

View Full Version : [Tutorial - PHP] Introduction to PHP



FlashD
08-14-2009, 04:30 PM
PHP Tutorial - Intro

Before we start you have to know PHP needs a basic understanding of HTML/XHTML. If you want to study these subjects first, find the tutorials in this forum.

OK, so ... PHP (PHP: Hypertext Preprocessor) is a widely-used, general-purpose scripting language that was originally designed for web development, to produce dynamic web pages. It can be embedded into HTML or work with a database and generally runs on a web server, which needs to be configured to process PHP code and create web page content from it. It can be deployed on most web servers and on almost every operating system and platform free of charge.

Installing PHP

If you have a web hosting plan you don't have to do anything, since most web hosts support PHP already, because it's free. Just create some .php files in your web directory, and the server will parse them for you.

However, if your server does not support PHP, you must install a webserver and the PHP module.

Installation on Linux:

If you use a Debian based Linux distribution you can use apt-get to install Apache and the PHP module.
Just use these two commands:

apt-get install apache2
apt-get install libapache2-mod-php5 php5-cli php5-common

Remember to log in as root before executing apt-get!


If you are not a Debian based system user download the Apache and the PHP module for Unix from here:
Apache: http://httpd.apache.org/download.cgi
PHP: http://www.php.net/downloads.php

After you have downloaded both files extract them with these two commands:

gzip -d httpd-2.2.13.tar.gz
tar xvf httpd-2.2.13.tar
and

gunzip php-5.3.0.tar.gz
tar -xvf php-5.3.0.tar

When the extractions are completed go inside the "httpd-2.2.13" folder and type these 3 commands:

./configure --enable-so
make
make install

With this you have successfully installed Apache.

Now you have to install the PHP module by going inside the "php-5.3.0" folder and type:

./configure --with-apxs2=/usr/local/apache2/bin/apxs --prefix=/usr/local/apache/php --with-config-file-path=/usr/local/apache/php --with-mysql
make
make install

If you are not planning to install the MySQL database later you can ommit the "--with-mysql" part.

Now we have to setup our PHP.ini file. Just type and execute:

cp -p php.ini-recommended /usr/local/apache/php/php.ini

Open and add these directives in /usr/local/apache/conf/httpd.conf (if already there, verify they are correct):

# Make sure there's only **1** line for each of these 2 directives:
# Use for PHP 4.x:
#LoadModule php4_module modules/libphp4.so
#AddHandler php-script php

# Use for PHP 5.x:
LoadModule php5_module modules/libphp5.so
AddHandler php5-script php

# Add index.php to your DirectoryIndex line:
DirectoryIndex index.html index.php

AddType text/html php

# PHP Syntax Coloring
# (optional but useful for reading PHP source for debugging):
AddType application/x-httpd-php-source phps

With this you have Apache configured to work with PHP. Now all you have to do is start the webserver. You do it by typing this:

/usr/local/apache/bin/apachectl start

To stop the server you can execute the stop command:

/usr/local/apache/bin/apachectl stop

Now open your web browser and type in the URL bar "http://localhost" and press Enter on your keyboard.
If everything works go to /var/www/ and copy your own PHP code inside the folder.


Installation on Windows:

For Windows user the installation is much simpler. Just download this file and install it:
http://www.wampserver.com/en/download.php

Wamp Server installs Apache, MySQL, PHP and PHPMyAdmin on your system.

Now open your web browser and type in the URL bar "http://localhost" and press Enter on your keyboard.
If everything works go to C:\wamp\www\ and test your own PHP code.


PHP Tutorial:


The scripting block

A PHP scripting block always starts with "<?php" and ends with "?>". It 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, it is recommended that you use the standard form "<?php" rather than the shorthand form.

Example:

<?php
?>

A PHP file normally contains HTML tags, just like an HTML file, and some PHP scripting code.
Below, 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>

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.

The output

There are two basic statements to output text with PHP: echo and print.


<?php
print "Hello World! <br />";
echo "Hello World!";
?>

Returns:

Hello World!
Hello World!

The above example shows both outputs are the same. It also shows that regular HTML tags may be placed within the value areas as well as regular text.

There is a slight difference between print and echo which would depend on how you want to use the outcome. Using the print method can return a true/false value. This may be helpful during a script execution of some sort. Echo does not return a value, but has been considered as a faster executed command. All this can get into a rather complicated discussion, so for now, you can just use whichever one you prefer.

Some resources will state that the quoted areas should be within rounded brackets. This is a valid way of coding, but not required.


<?php
print ("Hello World!");
?>

If you need to use quotes within your value area and have them show up on the web page, you will have to "escape" them. This is so the coding does not get confused on which quotes are the real ones and which ones are for display.


<?php
print "Hello from \"another\" World!";
?>

This will return:

Hello from "another" World!

Escape characters

Escape characters are small special functions. Most of them help in the visual presentation of the coding rather than the visual part on the web page.

As you have seen, you can easily add in regular HTML coding into the PRINT and ECHO commands. Thus being able to format the webpage contents to your desires. But what about the coding? It seems that a bunch of echo commands will just print out the coding on long continuous lines. Escape characters are here to help.

In PHP we have 3 types of escape characters:

New Line
Tab
Carriage Return


New Line:
The newline command is a very commonly used one when formatting the looks of your coding. It simply breaks the text into a new line. To use it, place the \n character where you want the text to break.

Before:

<?php
echo "This is a test. <br>";
echo "This is another test.";
?>

Your sourcecode output will look like this:

This is a test. <br>This is another test.

After:

<?php
echo "This is a test. <br>\n";
echo "This is another test.";
?>

Your sourcecode output will look like this:

This is a test. <br>
This is another test.

Tab:
The tab command is also helpful for those who prefer to use it throughout their coding for a "neater" layout style. This one positions your text more to the right. To call tab, use the \t character where you need your text spaced.

Before:

<?php
echo "This is a test. <br>";
echo "This is another test.";
?>

Your sourcecode output will look like this:

This is a test. <br>This is another test.

After:

<?php
echo "This is a test. <br>\t";
echo "This is another test.";
?>

Your sourcecode output will look like this:

This is a test. <br> This is another test.

Carriage Return:
The carriage return may come in handy later on when you are dealing with emails and printed materials. This special character that commands a printer or other sort of display to move the position of the cursor to the first position on the same line. The character used is an \r.

Comments

In PHP, we use // to make a single-line comment or /* and */ to make a large comment block.


<?php
//This is a comment

/*
This is
a comment
block
*/
?>

File names

The file must have a .php extension. If the file has a .html extension, the PHP code will not be executed.
You can also use extensions like .php3, .php4, .php5 which specify the version of the PHP functions used.

This concludes the first part of this tutorial. The next part will include variables and operators (http://www.animeforum.com/showthread.php?t=88734).

Balance
08-14-2009, 10:12 PM
Why this helps a lot FlashD.
Thanks, and I should really study this a bit more in order to have a greater understanding of the PHP that is used within my website.

I just don't know how my friend narunet managed to learn php all on his own...


Anyway, thanks again FlashD.
A few questions.
I've noticed that there are a lot of functions that are similar in php and java. Can you potentially use what you've learned from java and use it in php?

FlashD
08-15-2009, 04:32 AM
All functions in all languages are somewhat similar. JAVA's syntax is System.out.print() while PHP, C, Pearl use print(). Your knowledge from JAVA cannot be used for PHP, except for classes and even there it's only because the structure looks the same. So my answer would be "no, you can't".

Your JAVA knowledge can be used for JavaScript, though, which is the base for AJAX functions used in PHP. The whole PHP concept is more similar to C.

PHP is not hard to learn. I learned it on my own by lurking in the vBulletin code for Missy's site. :P