PHP Programming Basics
This article is the first of a series of PHP guides that aim at teaching you the basics of PHP programming.
In previous articles we have covered topics like
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>
//all statements goes here
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:
echo "PHP ! I am gonna get you.";
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?
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?
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,
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 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,
$message = "Hello Haroon";
echo "PHP Tutorial";
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,
$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?”
$question = "How are you?";
$name = "Haroon";
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