use of variables in php

In this tutorial you will learn how to use variables in php. In php variables are always declared with $ (Dollar) sign, it means every thing in php is a variable that have $ sign.


Variable Declaration in PHP

Here is the Syntax to declare the variables in php.
$name="MUHAMMAD ALI";
$val=1;

There are three things that need to be explained in above code segment.
  1. Variable always decalre with $ sign preceding it.
  2. Strings are always quoted. (Strings are combination of Characters)
  3. Integers are never quoted.
  4. There must be semicolon (;) after variable declaration.
  5. You can use any name for variable but be logistic when you want to give names to variables.
Note:- It is not compulsory in php to declare the variables before using it.


How to print the value of variables in php?


There are different ways to print the value of variables in php.
<?php $name="Muhammad Ali";?>
print $name;

Output will be Muhammad Ali
echo $name;

Output will be Muhammad Ali
printf($name);

Output will be Muhammad Ali


Use of html tags in php
Case Senario:- You want to print the name that is in $name variable in bold, here is the solution.

<?php echo "<strong>".$name."</strong>";?>


Note:- . is concationation operator.

Another method for this is
<?php echo "<strong>$name</strong>"; ?>


Another method is

<strong> <?php echo $name; ?></strong>


I hope you will enjoy this tutorial of using variables in php

0 comments: