Free Online Courses for Software Developers - MrBool
× Please, log in to give us a feedback. Click here to login
×

You must be logged to download. Click here to login

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

PHP Best Practices and Technique Patterns

In this article you will learn the server side programming languages which are being used for website developing, store data of websites at databases.

This article is fully dependent on the Basic understanding of PHP programming. PHP programming is a huge field of Server Side Programming Language, which couldn’t be complete by a single article. There are lot of books written but they are 75% on the basics of PHP programming not advance. If you learnt and completely understand the basic of a programming language you will your own learn the advanced programming in that language.

1. Handling Webpages Dinamically and Statically

When we want to design a website so there are two types of website pages or web pages, which is static and dynamic web pages. The static web pages take data which don’t change or may change but not using any database and we can also say that static web pages don’t interface with online database. But the dynamic web pages interface online database, it’s all data store in database and use MySQL to retrieve all data from database on the web pages. As we can make login pages, and registration pages and many other kind of web pages.

2. Techniques for Website Development

We use HTML, CSS, JavaScript, AJAX, PHP, MySQL, JQuery and many other techniques for developing web pages, to make web pages more attractive and more useful. As there are CMS (Content Management System), which are JOOMLA, WORDPRESS, DRUPAL. These three CMS are being commonly used in the market of website developing. Because these three CMS made the designing of web pages more easy. And these CMS help us to design dynamic web pages.

For dynamic web pages we use CMS if we want to design web pages easily and attractive, we can also design web pages by our own thoughts using PHP and MySQL. In which age we are living it is the time to create dynamic web pages. Of course, without using HTML, CSS, JavaScript, AJAX we can’t use just PHP and MySQL but the main thing is that make secure web pages which should secure data and be useful, which is done by PHP and MySQL.

3. PHP: The Server Side Programming Language

PHP is a language which is used as server side programming language. Programming with PHP is not a difficult job or task. Because if you learnt C/C++ programming so it will be not change for you but at some places you will feel lot of changes using of keywords as there is no any kind of data type or primitive type in PHP programming. PHP is an easy programming language for those people who learnt by heart the JavaScript client side web programming language, and fully experience on C/C++.

Php Logo

Figure 1: Php Logo

The first programming language which is understood by Human that is C/C++, that’s why other all programming languages else they are website programming or Desktop application programming languages have same features and using methods like C/C++. We can say the PHP programming is an Object Oriented programming, because it has also inheritance, polymorphism etc. techniques.

4. Getting Started

Writing code in any language is a fun, you will enjoy when your program runs as you want. I hope you will enjoy PHP programming. Before I start PHP Programming, I want to remind you one thing it is a server side programming language. So it’s mean that you need a localhost server when you are doing PHP programming. I use Xampp and I prefer Xampp to use it as your localhost server. When you install Xampp you can use MySQL also from it. It is a good web application and it is easy to use. If yet you didn’t install the Xampp windows server so visit our article and see how to install Xampp server in Windows .

5. Working with Xampp to Run our Apps

It is important to know that when you write your codes in PHP you should save it as .php extension and then choose your Xampp directory to save it in HTDOCS folder in Xampp. It is the path where I installed Xampp server, C:\xampp\htdocs. And when you write your program in PHP programming, then you will compile it using the http://localhost/yourprogramname link.

6. Basics of PHP Programming

We start PHP programming from <?php and end the code ?> as in C/C++ programming there are starting and ending braces “{}”. As I mentioned before there is no any data type in PHP programming. In PHP programming we declare variables using “$” sign i.e.

<?php 
	$name = “Sahara Baloch”;
?>

So in this simple program I just declare and assign variable and I named variable as “$name”. In PHP programming we can also use for starting code “” these two tags are called shot tag in PHP programming.

7. Using variables in PHP

In PHP programming as we told you that there is no any kind of primitive data types to use and declare variables. It is depend on the declaring and assigning of variables in PHP programming. When we need a variable to declare so we use “$” dollar sign to declare a variable. It is wasting of time in PHP programming to declare a variable and don’t use it at that time. Because in PHP programming we declare variables at that time when need it. As in C/C++ programming when we don’t need any variable but we can declare it before to use it, and we can use it at the end of the programming either we declared it at the beginning of the program. In C/C++ programming language there are four common data types which are being used as they are integer (int), character (char), decimal (float), and string. Let we use them in PHP programming.

<?php
$name = "Sahara Baloch";			 // string
$number = 0106;				 //integer
$gpa = 2.95; 					 //float
$grade= 'A';  					//char
?> 

We declared these variables and in comment I defined actually what they are? Let we print them on screen. As I told you that there is no any kind of primitive data type in PHP programming, what we declare it is the type of a variable in PHP.

8. Printing data in PHP

We can print data in PHP programming using “printf()” or “print()” and simple using “echo”. These are different from each other but “printf()” and “print()” are same. We commonly use “echo” function to print data in PHP, because it takes multiple parameters and does not return any value but the print() and printf() returns value. When we use “echo” to print data then we don’t use parenthesis we just use double quotes. We can use variables in two different types when we are printing data using echo, the first method is use concatenation operator that is dot product operator (.). And the second method is to use echo to print data and write the variable in double quotes.

Example:

echo “My name is ”.$name;                       //first method
echo “My name is $name”;                       //second method

9. Using comments in PHP

As we use single and multiline comments in Java and C/C++ programming languages. Same like that we can use in PHP programming. We use “//…” for single line comment and “/*… */” multi line comments.

A good programmer never forget to write or give comments in his programs. Because we can sell one program to million customers, how it is possible? It’s a trick. The trick is that when your customer contact with you to design a website, and you have designed a website same like that before for any other customer. And you have used comments in your programs, so it is now easy for you to do some changes and resell it.

10. First program in PHP

See the given program codes it is fully easy to understand it. Write it in notepad, notepad++ and any other application which you love to write programing codes on it. And then save it in .php formate.

Code:

<?php
$name = "Faisal Abdullah";
$grade = 'A';
$gpa = 2.95;

echo "My name is ".$name;
echo " and my grade is ".$grade;
echo " ,My gpa is ".$gpa;
?> 

OUTPUT

Output of echo

Figure 2: Output of echo

11. Compiling PHP programs

As you can see in Figure 2, the output of the program. Dear friends when you write your program in PHP programming, it is important where you should save it and how to compile it? You can see in Figure 2 at the address bar of the browser there is a link “localhost/mr.bool/1st.php”. There are three things to be understand.

Ist is localhost it is the Xampp directory link, by using it you can access your server. And the second thing is mr.bool it is the name of folder or file where the PHP program have been save. The last and 3rd thing is 1st.php, it is the name of the program which is saved in mr.bool folder.

The mr.bool folder is save in Xampp’s sub folder htdocs. There should be a question in your mind, if we save the programs in Xampp/htdocs so why we don’t use to create the link Xampp/htdocs/mr.bool/1st.php.

The answer is, we are using Xampp as a server and it is commonly used as a localhost server. And localhost mean that it is hosting your webpages locally. So Xampp/htdocs is the directory where you put the webpages either they are designed by PHP programming or any other techniques. We use to execute or access them by the localhost link and then sub directories as mr.bool and then your program or web page name.

A good programmer can write a single program in multiple ways because he knows and he has full grip on that programming language in which he is doing programs. As I mentioned before printing ways in PHP. So it is another way to write Figure 2 program by using printf and I used dot product operator, we can write same program without using dot product operator. Its output will be same but the codes in PHP will be change. Lets check.

Code:

<?php
$name = "Faisal Abdullah";
$grade = 'A';
$gpa = 2.95;

echo "Without using DOT PRODUCT OPERATOR<br>";
echo "My name is $name";
echo " and my grade is $grade";
echo " ,My gpa is $gpa";
?>

OUTPUT:

Another echo output

Figure 3: Another echo output

In Figure 3 I added a new line which is the first line of the program output. But the second line of the output is same as it was in Figure 2. And I didn’t use the dot product operator(.). Let we write this program another different way, just by replacing the echo into printf() or print() functions.

Code:

<?php
$name = "Faisal Abdullah";
$grade = 'A';
$gpa = 2.95;

printf( "Without using DOT PRODUCT OPERATOR<br>");
printf( "My name is $name");
printf( " and my grade is $grade");
printf( " ,My gpa is $gpa");
?>

OUTPUT:

Printf output

Figure 4: Printf output

You can see the Figure 2,3 and 4 there is not any change but in the way of writing the program is change. So we can say it we can write a single program in multiple ways. Writing programs in any language is logical not to cheat and copy past.

12. Checking Conditions in PHP

As we use different types of checking conditions in Java and C/C++ programming language as it is we can use in PHP programming. We can use if, if else, if elseif else and switch conditions same as it is being used in Java and C/C++ programming. Commonly

Syntax: For if condition

if (expression){
Statement...
}

Syntax: For if else condition

if (expression{
Statement…
} else{
Statement…
}

Syntax: For if elseif else condition

if(expression){
Statement…
}elseif(expression){
Statement…
}else{
Statement…
}

Syntax: For switch condition

switch(expression){
case:
Statement…
default:
Statement…
}
Loops in PHP programming:

The loops are used to complete the iterations in a program. There are types of loops, as for() loop, while() loop and do while() loop. Uses of these loops are same as they are being used in Java and C/C++ programming languages.

Syntax: For for() loop

for(expression start; expression compare; expression with increment or decrement){
Statement…
}

Syntax: For while() loop

while(expression){
Statement…
expression with increment or decrement
}

Syntax: For do while() loop

do{
Statement…
expression with increment or decrement
}while(expression)

Conclusion

In this article which is about the basic of PHP programming, I tried to make you understand how to write programs in PHP. I hope you understand this article and it is useful.



Web developer and passioned for web design, SEO and front end technologies.

What did you think of this post?
Services
[Close]
To have full access to this post (or download the associated files) you must have MrBool Credits.

  See the prices for this post in Mr.Bool Credits System below:

Individually – in this case the price for this post is US$ 0,00 (Buy it now)
in this case you will buy only this video by paying the full price with no discount.

Package of 10 credits - in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download few videos. In this plan you will receive a discount of 50% in each video. Subscribe for this package!

Package of 50 credits – in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download several videos. In this plan you will receive a discount of 83% in each video. Subscribe for this package!


> More info about MrBool Credits
[Close]
You must be logged to download.

Click here to login