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

No comments:

Post a Comment

Adbox