Infolinks

Breaking

Adsense

Tuesday, July 19, 2016

Introduction to PHP Tutorial

What is PHP?

PHP was developed by Rasmus Lerdorf in 1994.

PHP is a recursive acronym for PHP: Hypertext Preprocessor. It is widely used open source scripting language embedded into HTML that is suitable in developing dynamic web pages. It is compatible with most DBMS such as MySQL, Oracle, Sybase, etc.

PHP is a server side scripting language. Therefore, php scripts runs/executed on the server. You will be needing a software such as WAMP (Windows, Apache, MySQL, PHP), XAMPP, LAMP for Linux operating System and MAMP for Mac OS. If you are going to upload it online, you will be needing a server that runs apache, php, mysql  such as Apache or IIS.

You can download the latest version of WAMP Server on their official website. Or if you wish to use XAMPP which what I prefer, you can download it here.

Install it in your windows pc or if your using Linux then you install LAMP. To run your PHP, you need to look for XAMPP control panel. Make sure to install it first. Next, a form will display that looks like in the image below. Click on the start button that corresponds to Apache.


How PHP really works using this software? Let's take the following code as an example.

<!DOCTYPE html>
<html>
 <head>
  <title>Simple PHP Script</title>
 </head>
 <body>
  <?php echo "Hello World!";?>
 </body>
</html>

Your php script starts with the PHP start tag <?php and ends with a php end tag ?>
The code is being executed on the server and generating an html code back to the users web browser. The users will see a running script but will not be able to see the underlying php script.

A sample visual for you to easily understand. This is your php script on the server.


This is how the code looks like after passing the server.


And this is how it looks like in the user's browser.


Where to save your files in XAMPP?

You can create your php script in any text editor. In my case, I used the Notepad++. Save your file with a php file extension. Example sample.php and save it to the directory 

C:\xampp\htdocs\yourfoldername

For the WAMP, 

C:\wampp\www\yourfoldername

 Or you can visit one of my post related to XAMPP tutorial.

Displaying Output

PHP uses the echo() statement to display outputs like in the above example. Enclosed string with double quotation to display string values.

Common uses of PHP

  1. Perform system functions: create, open, read from, write to, and close files on your system; execute system commands; create directories; and modify permissions. 
  2. Gather data from forms: save the data to a file, send data via e-mail, and return manipulated data to the user. 
  3. Access databases and generate content on the fly, or create a Web interface for adding, deleting, and modifying elements within your database. 
  4. Set cookies and access cookie variables. 
  5. Start sessions and use session variables and objects. 
  6. Restrict access to sections of your Web site. 
  7. Create images on the fly.
  8. Encrypt data.

PHP Comments

Comments are used to document your codes in php or to test some part of the code for debugging purposes. Comments are being ignored by the parsing engine and are not executed. 

There are three (3) type of comments, //  and # for single line comment, and /* */ for multi line comment.

//This is a comment.
#This is a comment.
/*
This is a multi 
line comment.
*/

PHP Variables

PHP variables are used to store values such as string, numbers and arrays. All variables in php starts with a $ sign. The following are the naming conventions in creating variables in php.

1. It must start with a letter or an underscore ( _ ). 
Example: $first, $_last, $var1
2. It can only contain alphanumeric characters and underscore. (a-z, A-Z, 0-9, _).
Example: $num1, $first_name, $_num1
3. It should not contain spaces. If it is more than one (1) word, separate it with an ( _underscore such as $first_name or with capitalization $firstName.

4. It should be descriptive yet short.

5. It is case sensitive. Meaning, $a is not equal with $A. ($a<>$A)

Declaring Variables

<?php
  $text="Hello World!";
  echo $text;
?>

The script above will display Hello World! in your browser. If values are numbers, do not enclosed it with double quotation ("").
Example:

<?php
  $num=10;
  $pie=3.1416;
  echo $num;
  echo $pie;
?>

Code Cohabitation

Code cohabitation exist when you want to put html tags inside your php script. Since you cannot directly put html tags in php, you need to do echo the html tag and put it inside the double quotation ("") like in the below example.

<?php
  $num=10;
  $pie=3.1416;
  echo $num;
  echo "<br>";
  echo $pie;
?>



<?php
  $text="Hello World!";
  echo "<h1>$text</h1>";
?>

Note: Use double quotation "" if you want to display the actual value of variables. Use single '' quote to display the literal name of the variable.

String Concatenation

There will be cases wherein you need to combine two or more string values together. To achieved this, you need to use the concatenation operator which is the period ( . )

Example:

<?php
  $text1="Hello";
  $text2="World";
  echo $text1." ".$text2;
?>
The purpose of the " " is to put a space in between Hello World. Its like Hello + space + World.

Watch out for more articles related to programming and even systems administration. Please do hit the like button on the right sidebar of this page.

Introduction to PHP Tutorial

26 comments:

  1. Thanks for your informative post!!! After completing my graduation, I am confused whether to choose web design as my career. Your article helped me to make a right choice. PHP Training in Chennai

    ReplyDelete
  2. Very useful article.Some valuable tips on how to open URL in Google Chrome Help browser.Waiting for new updates.
    BE/B.Tech Project Center in Chennai | BE/B.Tech Project Center in Velachery

    ReplyDelete

  3. Thank you so much for sharing this worth able content with us. The concept taken here will be useful for my future programs and i will surely implement them in my study. Keep blogging article like this.
    Php course in chennai

    ReplyDelete
  4. Thanks for sharing this information and keep updating us. This information is very useful to me.Excellent Multimedia Summer Courses in Porur | Best Vacation Classes in Chennai

    ReplyDelete
  5. Pretty article! I found some useful information in your blog, it was awesome to read.
    Adobe Photoshop Vacation Classes in Chennai | Best Technical Boot Camp in Chennai

    ReplyDelete

  6. Great blog. You put Good stuff. All the topics were explained briefly. So quickly understand for me. I am waiting for your next fantastic blog. Thanks for sharing. Any course related details learn...
    Php course in chennai

    ReplyDelete
  7. Hey, Wow Provided Post will be Very much Informative to the People Who Visit this Site. Good Work! Thank You for Sharing.
    Joomla training in chennai

    ReplyDelete
  8. Nice and interesting article about "Introduction to PHP Tutorial".
    Data Science Training in Chennai

    ReplyDelete
  9. Thank you a lot for providing individuals with a very spectacular possibility to read critical reviews from this site.
    Good discussion. Thank you.
    Six Sigma Training in Abu Dhabi
    Six Sigma Training in Dammam
    Six Sigma Training in Riyadh

    ReplyDelete
  10. Online casino BGAOC big winnings and lots of lucky games are waiting for you. all slot machine games successful do not wait, make your luck smile to you.

    ReplyDelete
  11. let’s download the latest version of monster legends mod apk V9.2.2 for Android with offers unlimited coins, gems & everything. What do you think first when you hear about ‘monsters?’ Deadly devils that are thirsty for blood and ugly creatures full of strength and cruelty. But these terms alone do not define monsters as a whole. Monsters are also known for their thrill and mysterious nature. keep it up
    Ai & Artificial Intelligence Course in Chennai
    PHP Training in Chennai
    Ethical Hacking Course in Chennai Blue Prism Training in Chennai
    UiPath Training in Chennai

    ReplyDelete

Adbox