Tag Archives: PHP

PHP Logical Operator

Logical Operators

The Logical operators are used in PHP to determine the status of condition . When you use if…else or while statements, logical operator execute code based on the status of a condition .In this section, we will discuss only two logical operators:

The and operate (&&) is used when a statement has two condition to be checked. If both thecondition are true , the statement is true,.

The or operator ( || ) is also used when a statement has two condition is true, the statement is true.

  $a = 10;
  $b = 15;
  $c = $a + $b;
  if( ($a > 5) && $c < 20 ){
    echo "Value of c is less than 20";
  }else {
    echo "Value of c is more than 20"; 

The Output of the above code is: Value of c is more than 20

PHP Operators

Introducing Operators

In the previous section you learned about variables, data type, and constant and how to declare them. However, to use or declare variables or constants you need operators. The fallowing problem and itssolution explain the use of operators.

Identify the operators to be used

Operators are an integral part of any programming language, they are symbol that represent a specific action. For an example, the plus sign indicates that addition needs to be performed and is an operator use for addition. Using operators, you can perform a number of operation on a variable:
– Assign a value to a variable
– Change the value of a variable
– Compare two or more values
– Specify a condition

There are 6 basic operator categories:

– Arithmetic Operator
– Assignment Operator
– Comparison Operator
– Increment/decrement Operator
– String Operator
– Logical Operator

Arithmetic operators

Arithmetic operators are used to perform elementary mathematical calculation and are used when value of variable declare in integer type.

Name Example Output
Addition $a+$b The sum of $a and $b
Subtraction $a-$b The difference between $a and $b
Multiplication $a*$b The product of $a and $b
Division $a/$b The quotient of $a and $b
Modules $a%$b The remainder after dividing $a and $b

Let us look at some examples for a better understanding of arithmetic operators:

$a = 3;
$b = 6;
$c = $a+$b ; // The value of $c is now 9.
$a = 11;
$b = 2;
$c = $a-$b; //The value of $c is now 9.
$a = 3;
$b = 3;
$c = $a*$b; //The value of $c is now 9.
$a = 18;
$b = 2;
$c = $a/$b ; //The value of $c is 9.
$a = 9;
$b = 3;
$c = $a%$b; //The value of $c is now 0.

Assignment Operators

Assignment operators are use to assign a specific value to variable. They are also use to change the existing value of a variable by adding or subtracting from the variable ‘s current value. A few examples are =, +=, -= and .=.
In PHP you can use more than one assignment operator in a single statement, as shown below:

 $a = ($b=4) + ($c=5);

The variable $a, $b and $c are now integer types and will hold the values 9, 4 and 5, respectively.

Operator Usage Output
= (equal to) $a = 9; Assign value 9 to variable $a;
+= (plus equal to) $a += 9; Adds the original value assign to the variable $a to 9. Now the value of $a will be 9.
-= (minus equal to) $a += 3; Subtract the number specified from the original value of $a. Now the value of $a will be 6.
.= (dot equal to) $a .=” new numbers.”; (add) the new value specified with the original value of the variable. Now the value of $a will be 9 new numbers.

PHP Variables

Introducing Variables

What is Variable?

Variables are known to hold data that can change during runtime of a program. The following are few important factor about PHP variables
– A PHP variable should declared using a dollar sign followed by variable name.
– A variable name can start with an underscore or with a letter.

– PHP variables are case sensitive .So, $Web  is not the same as $web.

Naming convention

Similar to other programming language such as C and C++, PHP also fallows a naming convention that should be used while declaring variables. Consider the fallowing example

$myVal = "My First Variable";

The above examples shows the naming convention used in PHP to declare a variable:
– The dollar sign: In PHP, each variable that you declared should begin with a dollar sign.
– Variable name: It is always advisable to choose meaningful variable names. Which will helpdevelopers to understand why the variable was declared and where it can be used. For example, if you use the variable name $u for User name and $e for User email address then  you may misinterpret it at a latter stage. So, when you start using PHP or any other programming languages  you should use meaningful variable names.
– Operator : The equal sign is an operator that assigns a value to the variables.
– Data type (value assigned to a variable):  The string “My First Variable” is the value that assign to the variable $myVal.

Variables are assigned using values. These value can be of Boolean, integer, float, or string type. Oncedeclared, variables can be referenced in a script.

Introduction to PHP

What is PHP?

PHP stands for Hypertext Preprocessor. PHP is one of the first growing, free and platform independent server side scripting language. PHP is very easy to understand and user friendly programming language. The first version of PHP was released in 1994 by Rasmus Lerdof.

Sample PHP code

    echo "This is my first line.";

Lets examine this code line by line:

1. The first line is consist of opening of tag, <?php. And it is mandatory.

2. The second line has echo statement, which is used to display text in browser when we run the php file. All string in PHP should pass between single quote or double quote. The second line ends with a semicolon. You must use semicolon after each statement ends. The semicolon is known as the terminator in PHP. Otherwise it will display error when you run the code.

3. The third line consist of the closing tag, ?>.The closing tag marks the end of the PHP code and it ismandatory.

How to Embed PHP in HTML

Lets discuss how to embed PHP codes in HTML page.

<!DOCTYPE html>
<html lang="en">
<title>My First PHP Page</title>
<meta charset="utf-8">
   echo "This is my First PHP Page.";

PHP parse error

A parse error is come across when the code has syntax errors in it .For example, each line of PHP code should end with a semicolon. If you don’t specify a semicolon, you will get parse error, and the code will not be executed. Similarly, you will get a parse error when you forgot to uses double quotes for strings. Apart for this, there are several others options that might display parse error. The PHP parser tells the exert line on which it encounters a parse error. So you can easily find the mistakes and rectify it.

The following code will display error, because it is missing the semicolon termination.

    echo "This is my first line."

Escaping PHP Code

Escaping code is the process of overlooking instances of special characters in the code. The following code will display error. To avoid the error you can use an escape character (\) before double quote.

Wrong Code

  echo "Do you think, "You like php?"";

Correct Code

  echo "Do you think, \"You like php?\"";

Comment in PHP code

Comments are an primary part of any program. They are statement that are not executed along with the code. They help developers remember and recall why they have used a particular code block in a script.
There are four ways of specifying comments in PHP code:

<!-- You can put comment here -->

This is an HTML comment. Comments stated like this will be ignored by the browserand will not be interpreted.

//Put comment here.

This kinds of comments is commonly used within PHP codes.

#Put comment here.

The hash character can also be used to specify comments within PHP code.

Put comment here 
Here you can comment multiple lines