Introduction to PHP Part I
An Introduction to PHP I
Introduction
If you've decided to advance onto the world of server-side programming, it's time to look at PHP. PHP basically stands for Hypertext Preprocessor.The power of PHP extends how to however far you want to learn. If you wanted a simple script to update all pages on a website when the layout changes, you only need to know the basics, but when you push onto areas like OOP, Database-relation systems and XML/Ajax pre-performed tasks you need to be pushing into the advanced knowledge.
What will be in this article?
This article is written to give you a basic introduction into the PHP programming language. This is the very baby of the bad-boys.What do I need to know before-hand?
Basically, your server must be able to support .php, .phtml and/or .php3. The common extension is .php (as opposed to .html)PHP 4 is now out of date, and PHP 5 is now available. This tutorial and any others to follow will be simply focusing on PHP 4.
So, what do I do?
Creating a PHP file
You will probably want an editor for your PHP files. I, personally, use EditPlus3 for it's simple interface, FTP editing and the brilliant syntax highlighting system.All you have to do is create a file (it can even be done in Notepad) and save it as a .php file. Like .html files, index.php is the default index file.
Keep in mind that .php files cannot be previewed on your computer like .html files can. You'll need a server system such as Apache to view them.
Initiating a PHP script
If you've created your PHP file, then you'll need to know what signifies the start of a PHP script. Let's look at the example below
<html>
<head>
<title>My PHP page</title>
</head>
<body>
<?php echo "Hello world!"; ?>
</body>
</html>
The above code is but a simple message script. A PHP script can be placed anywhere on a page, and any code output created by the script will be placed where the code is at.
A PHP script is called by using the <?php and ?> tags. The <?php signifies the beginning of a PHP script, and the ?> signifies the end. You can call these repeatedly within a page, not just the once. Depending where you call them determines what order and where the output is placed.
Most PHP script support <? as an opening tag, which is fine. Most even support <?= as an echo identifier. This below codes will all perform the same function:
<?php echo "Hello world!"; ?>
<? echo "Hello world!"; ?>
<?= "Hello world!"; ?>
It just depends on your coding preference. I personally prefer to use <?php because I can see where a PHP script is being called easier
Some simple commands
Let's just run through a couple of basic functions. The first would be the echo command you just saw. This does what the function states, prints (or "echoes") text onto the screen at that specific location. Usage:
<?php echo "My message to display on the page"; ?>
The next basic command is the if() command. The if() command basically determines the code inside the brackets, and if the final, overall value is true (as opposed to false), then the script after it is performed. Otherwise, it is ignored.
<?php
if(true) {
echo "The value is true!";
}
?>
As you can see above, if the value is true then the code runs and echoes "The value is true!". Any codes within the { } brackets are ran.
If you want to define an alternative command to run if the value is not true then you use the else tag.
<?php
if(true) {
echo "The value is true!";
} else {
echo "The value is not true!";
}
?>
Conclusion
For now, i'll leave this as is. The next chapter will cover functions such as for(x;y;z), while(expression) and foreach(x as y => z) loops.I hope this tutorial has helped in basically introducing you to the world of PHP. As more tutorials are created, we will look into more and more advanced scripts.
