Posted By:
7 Mar, 2014 11:47 am PST

How to declare Variables in php?

How to declare Variables in php?

 

As name suggest, an entity who`s value can be changed. During processing u need to process data, so this data is stored in memory with the help of variable. A variable is a keyword or phrase that acts as an identifier for a value stored in a system’s memory The declaration preceded by ‘$’(dollar) sign followed by variable name with leaving any blank space in between.

Syntax: -

$Variable_Name; or

$variable_Name= Value ;

 

Example:-

$name;

$name=”hello world”;

$intNum=10;

$floatNum=28.3334;

 

Example

// to add, subtract, multiply, divide two numbers and display the result

<?PHP

$num1 =15;

$num2 =20;

$result = $num1 + $num2 ;

echo ’ <h2 > ’; // html tags

 

echo ’ Addtion of ’. $num1 . ’ and ’. $num2 . ’ is ’. $result ;

operator is used to print value of .( Dot ) variable←

 

echo ’ <br > ’;

$num1 =105.655;

$num2 =158.8594;

$result = $num1 - $num2 ;

echo ’ Subtraction of ’. $num1 . ’ and ’. $num2 . ’ is ’. $result ;

echo ’ <br > ’;

$num1 = -105;

$num2 =158.8594;

$result = $num1 * $num2 ;

echo " Multiplication of { $num1 } and { $num2 } is { $result } " ; // different way printing values

 

 

echo ’ <br > ’;

$num1 =105;

$num2 =0.5;

$result = $num1 / $num2 ;

echo ’ Division of ’. $num1 . ’ and ’. $num2 . ’ is ’. $result ;

 

?>

Variable.php

 

DeclareVariable 

Attachment: Variable.php


How to declare variable in PHP

 

Variables are capable to store information in them.In PHP all the variables begin with a dollar sign "$".It can store values like string,integer,boolean etc.

There is no need to declare data type before adding a value to it.A variable is created at the same moment you provide it a value,there is no function or method to declare a variable

When creating and declaring a variable certain rules are to be followed:

    »Variable must start with letter or underscore(_).

     »Variable with more than one word should be separated by underscore(_).

     »PHP variables are case sensitive.

     »It can have numeric(0-9), letter(a-z, A-Z) or underscore"_"

//Declaring variable in PHP

<!DOCTYPE html>

<html>

<body>

<?php

$str="WELCOME TO PHP PROGRAMMING!";

$a=12;

$b=8.2;

echo $str;

echo "<br>"."Value of a = ",$a;

echo "<br>"."Value of b = ",$b

;$c=$a+$b;

echo "<br>"."Value of c = ",$c;

?>

</body>

</html>

 
/*Output of the above program is:

 WELCOME TO PHP PROGRAMMING!Value of a = 12Value of b = 8.2Value of c = 20.2

*/ 

Attachment: Variable.php
Was it useful?  




Please login to reply to this problem.



Related Problems on Fixoncloud