variables and data types ,operators

variables in php:

variables are like containers those are used to store the data.

$var_name=value;

echo"hello world ";

$name="sam";

echo"name is ".$name;


$name="alax";

echo"name is ".$name;


constant variables: these types of variables are created by define function .data of the constant variables cannot be changed.

define(var_name,value,case-insensitve)


example:

define("age",54367);

echo age;


/*define("age",8497);

echo age;

this will throw an error

*/


1. always start the name of a variable with "$".

2. do not create variables using:

    • special characters
      • $n@ame
    • space:
      • $first name
    • variable name starting with number
      • $1number

data type:

  • string
    • $a="sam";  
  • int
    • $a=123;  

  • float
    • $a=5.6 
  • resource 
  • object
    • $car=new Car(); 
  • null
    • $a=null;   
  • array:
    • $a=[4,5,4,6,4,6] ; or
    • $A=array(); 
  • boolean:
    • $a=true;
    • $a=false;  

note: var_dump() can be used to show the data_type of a data or variable


operators:

assignment operators

=

+=

-=

*=

/=

%=

**=



arithmetic operators:

+:addition

-:subtraction

*:multiplication

/:quotient

%:reminder

**:exponential

example:

$a=3;

$b=2;


echo"addition",$a+$b,"<br>";

echo"subtraction",$a-$b,"<br>";

echo"quotient",$a/$b,"<br>";

echo"reminder",$a%$b,"<br>";

echo"multiplication",$a*$b,"<br>";

echo"power(exponential)",$a**$b,"<br>";


$a=10;

$b=20;


comparison:returns true or false based on the condition

>:returns true if  left hand side value is greater than right hand side value otherwise false

10>12 returns false

10>10 returns false

12>10 returns true



<:

returns true if  left hand side value is less than right hand side value otherwise false

10<12 returns true

10<10 returns false

12<10 returns false




>=:returns true if  left hand side value is greater than or equal to right hand side value otherwise false

10>=12 returns false

10>=10 returns true

12>=10 returns true



<=:returns true if  left hand side value is less than or equal to right hand side value otherwise false

10<=12 returns true

10<=10 returns true

12<=10 returns false


==:returns true if both left hand side value is equal to right hand side value otherwise false

10==12 returns false

10==10 returns true

"10"==10 returns true


===:returns true both  left hand side value is equal to right hand side value and also checks the data type otherwise false

10===12 returns false

10===10 returns true

"10"===10 returns false


!=: returns true if both let and right hand side values are not equal .

10!=12 returns true

10!=10 returns false

"10"!=10 returns false




!==: returns true if both let and right hand side values and data types are not equal and same .

10!==12 returns true

10!==10 returns false

"10"!==10 returns true



logical operators:

and

and        condition1   condition2    result

case1 true              false            false

case2         true               true            true

case3          false            true             false

case4          false            false            false



or

or           condition1   condition2      result

case1     true             false           true

case2          true              true            true

case3          false            true             true

case4          false            false           false


not: logical NOT operator returns true if the operand is false and returns false if the operand is true.

<?php 

var_dump(! 9>8)

?>

output:

bool(false)


sample questions to check the data is:

1.odd or even

2.positive or negative

3.vowel or not

4.alphabet or not



0 Comments