Sunday, 30 June 2019

Ch-5 Identifier in PHP

Ch-5 Identifier  in PHP
Like C and other programming languages  we need to declare and define identifier in PHP also, to create memory name and store data in that particular memory name space.
But in PHP we have no need to specify data type of identifier.
  Identifier Declaration Rule:
         <?php
$a=10;
echo $a
?>
Output-10
1.  Start with any letter, number,underscore
(remember, can not use any other special character, only underscore allowed ) eg. $1a;$_a;
2.  Identifier is a case sensitive.eg. $a=10, $A=20; both are different
3.  Identifier name cannot be identical; means it cannot be match by predefine keyword.
    There are 2 type of Identifier:
1.  Variable
2.  Constant
Variable:
a.  Variable name followed by $ sign. For eg.
$a; here a is an variable declaration.
b.  Variable can be change as so many time for eg.
i.                  $a=10; , php automatic create integer type memory location and store 10 .
ii.               $a=15.8; , php automatic create floating type memory location and store 15.8 in place of 10.
<?php
$a=10;
echo $a;
$a=15.8;
echo nl2br("\n$a");
?>
Output
10
15.8
First time $a store 10, then it store 15.8.
Here nl2br is a line break or new line function
Remember, like C , \n can not directly use with echo

Constant:
a.  Constant value cannot be change during program life.
b.  Constant name cannot be followed by $ sign.
c.   Only starts with any letter or underscore.
d.  For create identifier as a constant ,we need define() function, with 2 argument:
i.                  Constant name and
ii.               Value of that constant
For ex. Define(‘a’,10);
a is a constant name and 10 is a constant value of a.
Difference between variable and reference variable
From above we already discussed about variable that, variable name stats with a 4 sign which hold such a value which can be edit or change.
Like a variable, there is a another type of variable available in php , known as Reference variable.
Reference variable starts with double dollar ($$) ,this variable is used for reference of predefine variable value.
With the help of variable reference we can change another related variable value.
For ex.
<html>
<head><title>strtest</title></head>
<body bgcolor="red" text="green">
<?php
$x="abc";
$$x=20;

echo '$x is'.$x,'  <br>$abc is',$abc,' <br>$$x is'.$$x;
echo "<br><br><br>";

echo "if I will change $abc is 80 by ($abc=80;) then further value will be";

$abc=80;
echo "<br> <br> <br> <br> ";

echo ' now the value of $abc is'. $abc ,' <br> new value of $$x is'.
 $$x ,'<br> $x is '. $x ;
?>
$x isabc 
$abc is20
$$x is20



if I will change 20 is 80 by (20=80;) then further value will be



now the value of $abc is80 
new value of $$x is80
$x is abc
change in $abc value also changes $$x value

There are also many way to print string in php , you can also code such above program like :
<html>
<head><title>strtest</title></head>
<body bgcolor="red" text="green">
<?php
$x="abc";
$$x=20;
?>
<p>value of $x is <?php echo $x ?> <br> value of $$x is <?php echo $$x ?>
and<br> $abc is <?php echo $abc ?><br></p>
<?php
$abc=80;
echo "<br>";
echo $$x;
?>
<p> now the value of $abc is <?php echo $abc ?> <br> new value of $$x is
 <?php echo $$x ?><br> $x is <?php echo $x ?></p>
</body>
</html>

Output:
value of $x is abc
value of $$x is 20and
$abc is 20
80
now the value of $abc is 80
new value of $$x is 80
$x is abc
OR
In above 2 program you can use html character set value in place of $ sign. For eg.
<?php
$x=90;
echo "&#36x value is $x";
?>
This program print :  $x value is 90

No comments:

Post a Comment