PHP Variables - Code

PHP Comments Previous Next PHP Variables Naming


PHP variables are used for storing values, like text strings, numbers, or arrays.

All Variables in PHP start with a '$' sign symbol.

$variable_name=value;

Eg: Let us declared one string variable and one number variable.

<?php
$txtx="Welcome to PHP";
$x=22;
?>

Naming Rules for Variables


  1. A variable name must start with a letter or an underscore "_"
  2. A variable name can oly contain alpha-numeric characters and underscore (a-z,A-Z,0-9,and_)
  3. A variable name should be seprated with an underscore ($my_string), or with capitalization ($myString).

Declaring PHP Variables


Following are the code of declaring php variables.

Filename: php / variables.php


Code
<?php
$var="Hello PHP Variables";
$vars='Hello PHP Variables';
$int=5;
$float=18.5;
echo $var;
echo "
"; echo $vars; echo "
"; echo $int; echo "
"; echo $float; echo "
"; ?>

Running Step.


Type Url - https://localhost/php/variables.php

PHP Concatenation Variables


. operator are used to concate two variables or multi variables in php.

You can concate more than two variables.

Following are the code of concatenation of php variables.

Filename: php / concat-variables.php


Code C1
<?php
$var="Hello";
$var1="PHP Concatenation Variables";
$concat=$var.$var1; 
echo $concat;  // Output is Hello PHP Concatenation Variables; 
?>

Running Step.


Type Url - https://localhost/php/concat-variables.php

Following are the code of concatenation of multi variables.

Filename: php / multi-concat-variables.php


Code C2
<?php
$var="Hello";
$var1="PHP";
$var3="Concatenation";
$var4="Variables";
$concat=$var.$var1.$var2.$var3; 
echo $concat;// Output is Hello PHP Concatenation Variables; 
?>

Running Step.


Type Url - https://localhost/php/multi-concat-variables.php

PHP Case Sensitive Variables


Note: Php variables are case sensitive means $a and $A are not same variables.

Following are the code of php case sensitive variables.

Filename: php / sensitive-variables.php


Code
<?php
$a=400;
$A=80;
echo $a; // Output is 400;
echo $A; // Output is 80;
?>

Running Step.


Type Url - https://localhost/php/sensitive-variables.php

PHP Arithmetic Variables


It iss an mathematic calculation of two variables.

There are 4 type of arithmatic variables given below.

1.Sum of two variables in PHP.

2.Substraction of two variables in PHP.

3.Multiplication of two variables in PHP.

4.Division of two variables in PHP.

Following are the code of arithmatic variables.

1.Following are the code of sum of two variables in PHP.

Filename: php / addition-variables.php


Code A1
<?php
$a=5;
$b=10;
echo $a + $b; // Output is 15;
?>

Running Step.


Type Url - https://localhost/php/addition-variables.php

2.Following are the code of subtraction of two variables in PHP .

Filename: php / substraction-variables.php


Code A2
<?php
$a=40;
$b=10; 
echo $a-b; // Output is 30;
?>

Running Step.


Type Url - https://localhost/php/substraction-variables.php

3.Following are the code of multiplication of two variables in PHP.

Filename: php / multiplication-variables.php


Code A3
<?php
$a=35;
$b=34;
echo $a * b; // Output is 1190;
?>

Running Step.


Type Url - https://localhost/php/multiplication-variables.php

4.Following are the code of division of two variables in PHP .

Filename: php / division-variables.php


Code A4
<?php
$a=40;
$b=8;
echo $a/$b; // Output is 5;
?>

Running Step.


Type Url - https://localhost/php/division-variables.php



Tags: PHP Variables PHP Code Programming Language


Was this helpful ?