Introduction to PHP

Hello friends,

From last one week, I am getting a lot of requests from people for PHP. Here I am starting a PHP tutorial series. If you like these tutorial then please subscribe and share these tutorials.

Show all tutorials of this series till now

What is PHP?

PHP stands for PHP: Hypertext Preprocessor. PHP is known as a server side scripting and interpreter language for web development. It means PHP is used on the server to render dynamic web pages. Dynamic web pages mean those pages which are created or modified during rendering from server to client. If you don't know what is server and what is client then open new tab in your browser and type http://findalltogether.com . Now you are client and from where the page is coming that is the server (web server).

Installation:

You can easily install WAMP ( windows), LAMP (linux) or XAMMP (windows, mac and linux) according to your operating system. I am not telling you every step for installation because youtube already has a lot of videos of this purpose. I will prefer you to install XAMMP now and after learning basics, you should change for better understanding with servers. If you find some difficulty then ask me in comments. The php scripts run only in web browsers.

Syntax:

Lets start coding.

  1. <?php
  2. /* author: Harish Kumar
  3. website: findalltogether.com
  4. version: 1.0
  5. date: Mar 20 2015
  6. */
  7. // this is single line comment
  8. // comments are ignored by program/compiler
  9. $variable = 'variable';
  10. // we use echo statement for printing
  11. echo "string";
  12. echo $variable;
  13. ?>

Copy and paste this code in a file named index.php inside your htdocs (on windows) or www (on linux) folder. Open this file in your browser (after starting server) you will get result as stringvariablename . Now try to understand the code.

Syntax:

  • PHP code is written in a file with extension .php
  • PHP code is written between <?php and ?>
  • $ is symbol of variable (we will study about variable in this article later)
  • Each assignment statement ends with semi-colon ';'
  • single quotes and double quotes are equivalent (not equal) in PHP
  • echo command is used to print the data in a web page.

Comments:

The comments are the part of program which is not considered by compiler. These are the notes to remember/ understand the code later. There are two types of comments:

  • Single line comment: These comments are short comments of size one line. These comments are started with '//'.
  • Multiline comments: These are long size comment and may take more than one line. These comments are placed between /* and */

You can see both types of comments in the above example.

Variables:

Variables are which let you store and manipulate data in your scripts. You can simply assume them like maths variables. A variable is simply a container that holds a certain value. Variables get their name because that certain value can change throughout the execution of the script. It’s this ability to contain changing values that make variables so useful.

As I have told before the variables name start with $. Some valid examples of variable name are :

  • $simple
  • $_start_with_unserscore
  • $_includes_numbers_23
  • $_includes_UPPERCASE_2
  • $_23_it_is_valid

Note: you can not start a variable name with a number like $2_invalid_variable_name.

Data types:

Data types are the types of variables in PHP. In PHP, simple data types are integers (integer numbers), floats (decimal numbers), string (text line) and boolean (true or false). There are some more data types like objects and array, but we will study later about them.
Note: If you have strong type language background like C, C++, Java, C# etc. then you should note that PHP is weak/ loosly typing language, i.e. you don't have to tell which data type is stored in the variable.

Constants:

You can also define fixed value variables. These variables can not be changed after defining once.

  1. <?php
  2. /* author: Harish Kumar
  3. website: findalltogether.com
  4. version: 1.0
  5. date: Mar 20 2015
  6. */
  7. // this is single line comment
  8. $variable = 'variablename';
  9. echo "string";
  10. echo $variable;
  11. define( "MY_CONSTANT", "19" );
  12. define('PI', 3.14);
  13. echo PI;
  14. echo MY_CONSTANT;
  15. ?>

Here PI and MY_CONSTANT are fixed variables.

Operators:

PHP has many types of operators. Most :

  1. Arithmetic
  2. Assignment
  3. Bitwise
  4. Comparison
  5. Incrementing/Decrementing
  6. String

Arithmetic operators are your maths operators. These are +, -, *, / and %

Example:

$a = 4 + 5; // + is an arithmetic operator

Note: there is a difference between '/' and '%' operator. '/' is divide operator while % is modulus operator.

Example: 8 / 5 = 1.6 ( 8 = 5*1 + 3) while 8 % 5 = 3 ( 3 is reminder)

Assignment operator assigns values to variables.

Example: $a = 4; ('=' is an operator)

Bitwise operators performs operations on individual bits in an integer. Bitwise operators are & (and), | (or), << (left sift) , >> (right sift), ^ (XOR) and ~ (NOR).

Example: 14 & 3 = 2 (00001110 & 00000011 = 00000010 these numbers are in binary).

Comparison operators are used to compare one operand to another.

Screenshot-21

Incrementing/ decrementing operators are for increasing/decreasing the value of the variable by one unit.

Example:

++$x; // Adds one to $x and then returns the result
$x++; // Returns $x and then adds one to it
–-$x; // Subtracts one from $x and then returns the result
$x–-; // Returns $x and then subtracts one from it

String operatior is use for concat two srings or variables. For example use following code:

  1. <?php
  2. $a = 4;
  3. $q = 5;
  4. $c = $a.$q; // $a and $q are joined as string
  5. echo $c;
  6. $n = 'myname';
  7. echo "string ".$n.' $n inserted'; // joining three things
  8. ?>

Exercise:

  1. Try to define and print your own variables
  2. Try to use various operators on your variables and print the result

In next tutorial we will study about conditional statements in PHP. If you like it then please share it with your friend otherwise give your valuable feedback to improve this as well as future tutorial to help other people.

Don't forget to Subscribe for latest feeds.

Comments and feedback are welcome.

next tutorial



About Harish Kumar

Harish is an interested person in the field of web development and blogging. He works for the need of young web developers in learning various languages, latest technologies and other essential tips and tricks. If you need some help or you have some suggestion then you email him at harish@findalltogether.com without any hesitation. You can also suggest/demand for articles of your own choice.

Related Articles

In this part of free tutorial series, I am telling you about syntax, variables, data types, comments, constants and o...
Introduction to PHP
In this part of free step by step tutorial series I will teach you about for loop, while loop and do while loop of PH...
loops in PHP
In this part of free step by step tutorial series I will teach you about if, if else, nested and switch statements of...
Conditional statements in php

Login or Sign up to leave comment.