PHP Variables Define or Declaration with HTML Syntax

PHP Variables

Variable is used to store a piece of information or data to use this information or data throughout the PHP page. In PHP variable name starts with $ symbol.

Example 1 – Variables use and code with echo

<!DOCTYPE html>
<html>
  <head>
   
  </head>
    
  <body>
	<?php
		$x = 5;
		$y = 10.5;
		
		echo $x;
		echo "<br>";
		echo $y;
	?>
  </body>
</html>

In the above examples, we declare two variables($x, $y) and also print these variables.

Variables
Fig.1 – Variables

Important Facts About The Variables

  • In PHP the variables must be started from the “$” symbol
  • In PHP the name of variables must be started to the characters or underscore
  • In PHP the name of variables cannot start to a number it always starts to be a letter.
  • In PHP the name of variables content only alpha-numeric characters and underscore.
  • In PHP the name of variables are case-sensitive.

Caution:Case-Sensitive means that if the name will have capital latter than you must have to write that character to be a capital you cannot write into a small character.

How To Shows the Value of Variables on the Web Page

An echo is used to get the output of variables on the the web page

Example 2 – Variable Declaration And Print the Value in the final HTML output

<!DOCTYPE html>
<html>
  <head>
   
  </head>
    
  <body>
	<?php
		$age = "5years";
		$a = 10.5;
		$c= 7;
		
		echo $a;
		echo "<br>";
		echo $c;
		echo "<br>";
		echo $age;
	?>
  </body>
</html>

In the above examples, we declare three variables($age, $a, $c) and take the output of variables on the web page using echo.

PHP Variables Define or Declaration with Output in HTML Syntax
Fig.2 – Variables Declaration and Output.

Types of PHP Variables Define and Declaration and Print Value in HTML Syntax

Followings are the types of variables in PHP.

  • String
  • Integer
  • Float/Double
  • Boolean
  • Array
  • Object
  • NULL
  • Resource

But the PHP is a loosely typed language. In the above two examples we did not declare the type of variable it automatically takes the variable in the correct type of variables.

Example 3 – PHP Variable Declaration And Output with echo function

<!DOCTYPE html>
<html>
  <head>
   
  </head>
    
  <body>
	<?php
		$text = "Today is too hot day.";
		$a = 5;
		$b = 6.7;
		$c = 'A';
		
		echo $a;
		echo "<br>";
		echo $b;
		echo "<br>";
		echo $c;
		echo "<br>";
		echo $text;
	?>
  </body>
</html>

In the above examples, we declare some variables($text, $a, $b, $c) and take the ouput of variables on the web page using echo.

Where “$a” is a integer type, “$b” is a float type, “$c” is a character type and “$text” is a string type variables.

But in the above examples, we can not declare the types of variables browser automatically detect which type of variables are written in the above example and shows it on the web page.

Variables Types
Fig.3 – PHP Variables Types Define
You can leave a response, or trackback from your own site.

2 Responses to “PHP Variables Define or Declaration with HTML Syntax”

  1. This design is incredible! You certainly know how to keep a reader entertained. Between your wit and your videos, I was almost moved to start my own blog (well, almost…HaHa!) Great job. I really enjoyed what you had to say, and more than that, how you presented it. Too cool!

  2. Elliott says:

    Very quickly this web site will be famous among all
    blogging and site-building visitors, due to it’s fastidious articles or reviews

Leave a Reply to the article


Learn PHP

Learning & Certifications
Follow Us
Facebook Icon   Linked In Icon   Twitter Icon  
Validation and Recognition

Valid CSS! Valid HTML5!          Protected by Copyscape