PHP for beginners – Introduction
PHP (Hypertext Preprocessor) is a widely used language these days and has large scale application. New to PHP? Dont worry, let us start with learning the basics of PHP.
What is PHP?
PHP is an open source server-side, cross-platform scripting language that stands for Hypertext Preprocessor. This means PHP scripts are executed on the web server and not from the browser, therefor eliminating compatibility issues. PHP is probably the most popular scripting language on the internet. It is used to provide more complex and enhanced web pages for better user experiences.
Why should I use PHP?
Using PHP on your website allows you to add more interactive features for your visitors ranging from guestbooks, feedback forms, bulliten boards, portal systems, content managing systems. The list of things PHP can be used for is never ending and most things can be accomplished.
What will I need?
As mentioned in What is PHP, PHP is a server-side scripting language this means that your visitors are not required to install any software. To be able to display PHP pages your websever will have to be setup with Apache and PHP, your webhost will be able to confirm this for you.
Writing your own PHP
Writing PHP is fairly simple you wont need any special programmes. PHP can be written in many text editors like notepad, although I recommend.
PHP for beginners – Intro
Declaring your PHP code
PHP is always enclosed with opening and closing tags, this tells the webserver to execute the code between them. You are able to do this in a variety of different ways.
<script language="php"> </script>
These all have the same concept in calling PHP. You must never forget to start and close your tags when using PHP.
What is PHPINFO
A phpinfo.php file is a file that makes it easier for you to see information about your server. While it doesn’t include version information about your database scheme, it does include version information about PHP, Apache/IIS, GD, and other things.
create a new “phpinfo.php” file with the following code
<?php phpinfo(); ?>
As you can see this is just one line of code. It is a standard PHP function called phpinfo which will tell the server to print out a the information of the server setup.
Testing your script
Save your newly created .php page and navigate to /phpinfo.php in your browser if your server supports PHP you will see alot of information about your systems config. Alot of webservers will have this function disabled for security reasons.
If you recieve a white page the function is disabled, if you are prompted to save the .php file PHP is not correctly setup.
Printing text on a page
To output text in your PHP script is actually very simple. As for most other things in PHP there is more then one way, but the most know way is ‘Print’. This will allow you to output text, variables or a combination of the two.
The print statement goes like this..
<?php print("Hello world!"); ?>
Print is the function that tells PHP to print the text or variables onto a web page, because you are outputting text, the text is also enclosed inside quotation marks and has to end in a semicolon.
As with other programming languages, PHP allows you to define variables. In PHP there are several variable types, but the most common is called a String. It can hold text and numbers. All strings begin with a $ sign. To assign some text to a string would go like so.
$welcome_text = "Hello and welcome to Tutfreak.";
This is quite simple to understand, everything inside the quotation marks will be assigned to the string. You must always remember a few rules about strings.
- Strings are case sensitive, so $Welcome_Text is not the same as $welcome_text.
- String names can contain letters, numbers and underscores but cannot begin with a number or underscore.
- When assigning numbers to strings you do not need to include the quotes.
$tut_number = 1234;
Outputting Variables To display a variable on the screen uses exactly the same code as to display text but in a slightly different form.
$welcome_text = "Hello and welcome to Tutfreak."; print($welcome_text);
The only major difference is that you do not need the quotation marks if you are printing a variable.