PHP Programming Basics 

This article is the first of a series of PHP guides that aim at teaching you the basics of PHP programming. 

Hi,

In previous articles we have covered topics like

How to install PHP on windows
How to install PHP on Linux
How to install Apache on Windows
PHP, MySQL, Apache installers

By now, I hope you would have set up your system to start actual PHP programming.

Lets start PHP programming.

What are tags?

Tags are the starting and ending point for a specific code snippet belonging to any scripting language. For example we close HTML codes between

<html> and </html>

similarly javascript code is enclosed like

<script language=javascript>
//all statements goes here
</script>

ASP (Active Server Pages) uses

<%
'ASP code goes here
%>

and similar is the case for other languages.

so you are thinking that why do we need tags? For this purpose first of all you have to understand the process of a web page display in your browser window i.e Internet Explorer (IE), Opera or FireFox (FF) etc etc

Web Page Parsing:

When you write any web page URL in your browser’s address bar e.g http://www.fastcreators.com and click enter; you in fact send a request to your web server that follows the URL and picks the contents from the target location of the requested web page.

Remember, whatever you see in your browser is HTML output of the actual code of the requested web page and it might not be the actual code specially in cases when a web page uses a client-side technology or server side scripting to process information to show you the output of a page.

This process of converting the actual code to the desired HTML output is called parsing by your web server’s parser engines.

For example, if you request the URL http://fastcreators.com/article/ you see my Articles Repository home page with the latest articles on the home page and available articles categories etc etc.

In fact you are watching the HTML output of a complex and long PHP code. When you request the URL the web server follows the location and request the contents from the Server that is contains the requested web page. All the web page code is compiled (parsed) and if any TAGS (be attentive) of some client or server side languages are found in the code the web server ask the related parser engine to take on this code and compile it accordingly to generate the HTML output from the processing. If the code doesn’t have any error or warning etc that can stop the output generation, the parser engine sends back the HTML output to the web server that is generated from the server or client side code. Once all the code contained in different TAGS is parsed to HTML Output the web server sends the resultant web page to your browser window for display.

Let stick to our articles repository request now to understand the tags and parsing in depth.

step 1: http://fastcreators.com/article/index.php is requested

step 2: Server that hosts this web page is requested to send the contents attached with this page.

step 3: web server examine the code for any additional processing, as our articles repository’s back-end is written in PHP therefore the web server will find the PHP TAGS in different places and as soon as it finds the PHP starting tag it will start sending those statements to the PHP parser Engine unless it finds the PHP ending tags for that specific code session.

step 4: web server asks the PHP parser engine to jump in and generate the HTML output from the php code in the page. As all the articles are stored in a MySQL database so the index.php code has necessary statements to fetch the latest articles info from the database. Hence all the information is fetched and the desired HTML output from all this PHP-MySQL integration and processing is returned to the web server from PHP Parser Engine and web server finally show the page that you see by visit my Articles Repository at http://fastcreators.com/article/index.php

So this is how important are the tags for any language in fact they informs the web server that they need to be processed by some parser engine before the final output is sent to the browser.

PHP Starting and Ending Tags:

Similar to other languages PHP code starts with <?php tage and ends on ?>

Lets write our very first PHP code:

<?php
 
echo "PHP ! I am gonna get you.";
 
?>

Code Explanation:

The above PHP code is very simple and contains only three lines,

first it start with PHP starting tag and after the crap written after it, it finally close the code session with PHP ending tag.

now lets discuss the crap :)

ECHO helps you to display any combination of string and variables on your page… wasn’t it simple?

Syntax:

echo “display message goes here”;

everything that you want to display on the page must be enclosed between either double quotes or single quotes, php allows both.

the semi-colon is used to define the end of a statement in PHP. Your first quick verbal assignment.

how will you output your “Hello YOUR NAME” message in php?
5..4…3…2…1

stop thinking and give me your answer… good work I know you have something inside you therefore you are learning PHP.

yes, for my name it should be,

<?php
echo "Hello Haroon Ahmad";
?>

to make sure things are working for you fine write different sentences and make a php page and browse it to check your php is working as well as you are understanding my lecture. Did you say how to browse a php page? well, make sure your PHP and Apache or other web server like IIS is running, then open a browser window and write http://localhost/your-php-pages-folder/yourpagename.php and this is how we execute PHP pages.

Now close your eyes for five minutes and go through all major points that we have learned till now in this lecture…

wake up guys ! … lets talk about few new friends…

Variables:

Variables are your friends, whenever you have something to store somewhere temporarily give it to the variables and they will keep it for you and whenever you need it call related variables and they will give it to you and you can change, add or delete the received information and return the new values to variables and they will not complaint at all :)

Who shouted BE SERIOUS? Ok, so to define a variable, seriously, it gives you the facility to reserve some space in the memory for different values to use them later in your script’s life time.

For example, I want to save the message “Hello Haroon” in memory and then first display “PHP Tutorial” and afterward the message I stored; I will write the following simple programme,

<?php
$message = "Hello Haroon";
  echo "PHP Tutorial";
echo $message;
?>

the output of this programme will be

PHP TutorialHello Haroon

No It is not a type but it is in fact how the output will look like, if you want the message to appear in the next line then put ‘\n’ after first message i.e

echo “PHP Tutorial \n”;

\n is used for Line Feed or next line. Remember on Linux the line feed is obtained by using \n however on windows you have to use \r\n

\r stands for carriage return and these special characters are called Escape Characters, anyway don’t confuse yourself we will study Escape Characters later in details.

In PHP we define a variable with $ (dollar) sign (dollars entered into our programming too :( )
PHP is a loose-typing scripting language and it doesn’t have any hard and fast rules for variables like C or Java languages have. You can define a variable with $ sign and parser itself determines the data-type of this variable at run time.

However PHP has seven types of variables, and all but one hold a specific class of information. The seven types are: strings, integers, floats, booleans, arrays, objects, and resources.

Strings hold characters (literally: a string of characters) such as “a”, “abc”, “Jack and Jill went up the hill to fetch a pail of water”, etc. Strings can be as short or as long as you want - there’s no limit to size.

Integers hold whole numbers, either positive or negative, such as 1, -20, 55028932, etc. There is a maximum limit to the size of integers - any numbers lower than -2147483647 and any numbers higher than 2147483647 are automatically converted to floats, which can hold a much larger range of values.

Floats hold fractional numbers as well as very high integer numbers, such as 4.2, 1.00000001, and 2147483647000.

Booleans simply hold true or false. Booleans are, in fact, just integers behind the scenes - PHP considers the number 0 to be false, and everything else to be true.

Arrays are a special variable type in that they hold multiple values. Arrays can be quite complicated, and so are covered in detail in their own chapter.

Objects are complex variables that have multiple values, but also their own functions. Objects are also very complicated, and, like arrays, are covered in their own chapter.

Resources are anything that is not PHP data - this might be picture data you have loaded from a file, the result of an SQL query, etc. Resources are used like any other variable, with the key difference that you should generally remember to free up your resources when you are finished with them.

Operators and Operands:

See the following PHP Code,

<?php
 
$salary = 3000;
$bonus = 1000;
$total_salary = $salary + $bonus;  // this line will be explained
 
echo "Total Salary is: " . $total_salary; // output: Total Salary is: 4000
 
?>

One other rule you should remember is that you cannot use spaces,dot or dashes in your variables name however you can use an under score.

Operands are the entities that have some values in them or you can say the variables and operators are the symbols that have special meanings in the language for example addition operator ( + )

Also in echo statement I used something new; Dot Operator: it is used for concatenation or to join different entities to create a final string for echo to print on the page.

The following will display “Hi Haroon, How are you?”

<?php
$question = "How are you?";
$name = "Haroon";
 
// concatenation
 
echo "Hi" . $name .", " . $question;
 
?>

It helps you to glue different values into a single statement.

With this we will end our today’s lecture. Please practice ! I agree with Shakespear, “Practice makes a man perfect” and programming is all about practice. To learn syntax of a programming is not even the start of the story you can call yourself a programmer only when you are able to create logic to solve some computer problem with less code and fast execution time and you can achieve this through practice. Programming practice polish your skills and gives you new ideas to solve different problems through strong logic.

Practice whatever you learn through my lectures before taking on the next lecture otherwise you are wasting your time.

In our next lecture we will learn the different data types of PHP and Complex use of Operators available in PHP in details.

Till then take good care of yourself and … practice :)

Regards,
Haroon Ahmad

Share this page:

12 Comment(s)

Add comment

Please register in our forum first to comment.

Comments

By:

Hi

I had a very good discussion on digg.com  on my this articles and a few follows pointed out my typos in the articles, for example defining Integer values as string and not putting $ sign with the variable 'bonus' in the salary example, I have fixed it on this site and also on the other sites. Thanks for your positive feedback.

At the same time I received some negative criticism from people that I want to answer.

1) a person criticised that why didn't I use "hellow world" as the first programme of php?

well, it is the most stupid thing I have heard from some person who is commenting a php article. Hello World programme has nothing to do with PHP and it is not must to post it as the first PHP programme.

2) Concatenation: I person said that why did I use echo "hello!" .", " . "Haroon"; and he talked about the use of comma to concatenate. Well both are correct, you can use dot or comma to concatenate strings and there is nothing wrong with it. You should follow what is comfortable for you.

3) Some said, why didn't I post articles to set up the php environment?

well, they should read my article again, in the very start of my article I have given links to four such articles in which I talk about installing php environment on windows and linux both.

anyway, I always pick the healthy criticism and it's most welcome. I will try to avoid typos in my coming articles. I love my approach of teaching PHP and I have thousands of students around the world who are following my articles on different technologies. I write articles for my own satisfaction. Though I am very open to healthy criticism, please do tell me if you think I can improve my writing or can add some additional stuff to my articles I will lovet o do so.

Regards,

Haroon Ahmad

By: SirOB

HI Haroon !

 

I'm beginner in the world of PHP, Apache and MySQl. (PAM)

But i have related with a development made in  PAM, over WINDOWS.

Now i have to migrate (i think, i could call it) from windows to linux, 

but i can't doit  that so easy.....

There is a tip or a general rule to do this?  I think its complicated....

Can you help me?

By: Joseph Piche

as a professional PHP programmer, I can say that there is a lot to PHP that this tutorial does not cover, but it's a wonderful start. Another great place to help switching from another environment is the php documentation at php.net.

By: Dip

This nice tutorial gives nice learning concept for beginner and intermediate developers. We published some good articles at our blog related to the above subject. Please, go through the link to learn more. We appreciate your feedback.

Thank you,

Dip

By: Rakshit

Nice tutorial as to start with php  i found this to be very very helpful http://bit.ly/4GXlC5

By:

Thank you for  guide

By: Rakshit

Nice tutorial as to start with php i found this to be very very helpful http://bit.ly/4GXlC5

By: kripssmart

Nice work with the tutorial...

Thanks!

Krips.

 

By: Website laten maken

Wonderful Tutorial I ever seen, the step by step really help any newbies.. thanks....

By: Jimmy

Great work. Easy to understand PHP basics for beginners. 

hi guys for the beginners they have to study hard and learn something to it, and explore codes.

By: Faruk Ahmed

Step by step discussion is very good. This article helps to know about PHP for the beginners. Thank you.