What are the basics of php?

PHP is the most popular server-side scripting language for creating dynamic web pages.

What are the basics of php?

PHP stands for Hypertext Preprocessor. PHP is a very popular and widely-used open source server-side scripting language to write dynamically generated web pages. PHP was originally created by Rasmus Lerdorf in 1994. It was initially known as Personal Home Page.

PHP scripts are executed on the server and the result is sent to the web browser as plain HTML. PHP can be integrated with the number of popular databases, including MySQL, PostgreSQL, Oracle, Microsoft SQL Server, Sybase, and so on. The current major version of PHP is 7. All of the code in this tutorial has been tested and validated against the most recent release of PHP 7.

PHP is very powerful language yet easy to learn and use. So bookmark this website and continued on.

Tip: Our PHP tutorial will help you to learn the fundamentals of the PHP scripting language, from the basic to advanced topics step-by-step. If you're a beginner, start with the basics and gradually move forward by learning a little bit every day.


What You Can Do with PHP

There are lot more things you can do with PHP.

  • You can generate pages and files dynamically.
  • You can create, open, read, write and close files on the server.
  • You can collect data from a web form such as user information, email, phone no, etc.
  • You can send emails to the users of your website.
  • You can send and receive cookies to track the visitor of your website.
  • You can store, delete, and modify information in your database.
  • You can restrict unauthorized access to your website.
  • You can encrypt data for safe transmission over internet.

The list does not end here, there are many other interesting things that you can do with PHP. You will learn about all of them in detail in upcoming chapters.


Advantages of PHP over Other Languages

If you're familiar with other server-side languages like ASP.NET or Java, you might be wondering what makes PHP so special. There are several advantages why one should choose PHP.

  • Easy to learn: PHP is easy to learn and use. For beginner programmers who just started out in web development, PHP is often considered as the preferable choice of language to learn.
  • Open source: PHP is an open-source project. It is developed and maintained by a worldwide community of developers who make its source code freely available to download and use.
  • Portability: PHP runs on various platforms such as Microsoft Windows, Linux, Mac OS, etc. and it is compatible with almost all servers used today such Apache, IIS, etc.
  • Fast Performance: Scripts written in PHP usually execute or runs faster than those written in other scripting languages like ASP, Ruby, Python, Java, etc.
  • Vast Community: Since PHP is supported by the worldwide community, finding help or documentation related to PHP online is extremely easy.

Tip: Do you know some huge websites like Facebook, Yahoo, Flickr, and Wikipedia are built using PHP. Most of the major content management systems (CMS), such as WordPress, Drupal, Joomla and Magento are also built in PHP.


What This Tutorial Covers

This PHP tutorial series covers all the fundamental programming concepts, including data types, operators, creating and using variables, generating outputs, structuring your code to make decisions in your programs or to loop over the same block of code multiple times, creating and manipulating strings and arrays, defining and calling functions, and so on.

Once you're comfortable with the basics, you'll move on to next level that explains the concept file system, sessions and cookies, dates and times, as well as how to send email from your script, handling and validating forms, perform data filtration and handling errors in PHP.

Finally, you'll explore some advanced concepts like classes and objects, parsing JSON data, pattern matching with regular expressions, exception handling as well as how to use PHP to manipulate data in MySQL database and create useful features like user login system, Ajax search, etc.

Tip: Every chapter in this tutorial contains lots of real-world examples that you can try and test using an online editor. These examples will help you to better understand the concept or topic. It also contains smart workarounds as well as useful tips and important notes.

On this page

  1. PHP Programming Basics 
    1. What are tags?
    2. Web Page Parsing:
    3. PHP Starting and Ending Tags:
    4. Lets write our very first PHP code:
    5. Code Explanation:
    6. Variables:
    7. Operators and Operands:

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

Suggested articles

12 Comment(s)

Comments

×

This feature is only available to subscribers. Get your subscription here.

What is the basic structure of PHP?

Basic PHP Syntax A PHP file normally contains HTML tags, and some PHP scripting code. Below, we have an example of a simple PHP file, with a PHP script that uses a built-in PHP function " echo " to output the text "Hello World!"

What are the main topics of PHP?

Key topics:.
Variables and operators..
Control structures, arrays and PHP array functions..
External files..
Functions, arguments, passing by reference, globals and scope..
OOP in PHP4 and PHP5..
MySQL database form PHP..
SQLite..
Sessions and Cookies..

What are the steps to learn PHP?

How to Learn PHP: Step-by-Step Guide.
Step 1: Learn HTML and CSS. PHP is a great programming language to learn, but it should not be the first technology you master in web development. ... .
Step 2: Master the Basics. ... .
Step 3: Build a Project. ... .
Step 4: Focus on Readability. ... .
Step 5: Join PHP Communities. ... .
Step 6: Get in Some Practice..