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.
- Variable always decalre with $ sign preceding it.
- Strings are always quoted. (Strings are combination of Characters)
- Integers are never quoted.
- There must be semicolon (;) after variable declaration.
- You can use any name for variable but be logistic when you want to give names to variables.
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:
Post a Comment