Using Variables, Strings, and Basic Operators in PHP

 

In PHP variables are always declared by using $ as a prefix, just like javascript,  php is also an untyped programming language because the typed of variable is defined after the execution of the code.  

Let’s see an example of the defining a variable in php: 

$txt = "Helloworld!";

$x = 5;

$y = 10.5;

?> 

In the above code, after the execution of the code, the $txt will be become string type of variable, $x will be an integer type and $y will be the float type variable. Variables are just like a containers for storing the data. 

For learning back end programming,joining web development course in Delhi is highly recommended. 

Strings in PHP 

When we put sequence of character stogether inside the inverted comma, it becomes a string. There are four ways to define a string in the php. 

1. SingleQuoted 

2. DoubleQuoted 

3. Heredoc Syntax 

4. Newdoc Syntax 

Here we will see example of only two types single and double quoted string of php: 

Single Quoted:  

      $str='Hello text with in single quote';    

      echo $str;    

?>  Output: Hello text within single quote   

Double Quoted: 

$str="Hello text within double quote";   

echo $str;   

?>   Output: Hello text with in double quote We can use single quotes inside double quotes string and vice versa. Basic Operators in PHP 

Some of the operators are listed below: 

· Arithmetic Operators 

· Assignment Operators 

· Comparison Operators 

· Increment/Decrement Operators 

· Logical Operators 

· String Operators 

· Array Operators 

· Conditional Assignment Operators 

In the above list some of operators are mentioned which are being used in the php, we will discuss about few of them here in details and will see example’s of them.    

Arithmetic Operator: 

Arithmetic operator is usedto perform mathematical tasks in the php code, such as addition, subtraction,multiplication and subtraction, modulus. 

Let’s see an example of additionin php:  

     $x=5;  

     $y=15;    

     echo $x+$y;    

?> 

Join PHP course in Delhi and attain professional skills of back end development. 

Assignment Operator: 

Assignment operator is used to assign values to variables, the value of right hand side will be assigned tothe left hand side variable. 

Let’s see an example of assignment operator:  

    $x=5;  

    $y=15;    

?>  

Comparision Operator: 

Comparision operator is used tocompare to values with each other, we have an equal operator and strict equal operator to compare values. Equal operator will just compare the both values and return true or false accordingly but strict equal will compare the values as well as dataype of both of the variables and will return the result as true or false accordingly. 

Let’s see an example of it:  

   $x=15;  

   $y=”15”;   

If($x==$y) 

{  

echo “Values is matched”; 

else 

 echo “Value is not same”; 

}    

?> 

Let’s see an example of strict equal here:    

   $x=15;  

   $y=”15”;   

If($x===$y) 

{  

echo “Values is matched”; 

else 

 echo “Value is not same”; 

}    

?> 

In the first example, the output will be the “Value is matched” because it will just check the value of thevariable and both are 15, so it will return true but in the strict equal case it will check the value as well as type of both variable so in this case $x is number but $y is string type of data it will return while comparing them. So the output will be not same value in strict. Join web development courses in Delhi and make your career in it.