How to PHP Scripting- Variables & Echo function

In this guide, we are going to walk through PHP variables & Echo function. Variable is a symbol or name that stands for a value. Here we have 3 main steps

1.PHP Variables and data types.
2.Output the Variables using Echo function.
3.Data Types in PHP

 

1.PHP Variables and data types.

Variables are “containers” for storing information. Variables are used for storing values such as numeric values, characters, character strings, or memory addresses so that they can be used in any part of the program.

Variables
       <?php
          $txt = "Hello world!";
          $x = 5;
          $y = 10.5;
       ?>

Here are the most important things to know about variables in PHP.

All variables in PHP are denoted with a leading dollar sign ($).
The value of a variable is the value of its most recent assignment.
Variables are assigned with the = operator, with the variable on the left-hand side and the expression to be evaluated on the right.
Variables can, but do not need, to be declared before assignment.
Variables in PHP do not have intrinsic types – a variable does not know in advance whether it will be used to store a number or a string of characters.
Variables used before they are assigned have default values.
PHP does a good job of automatically converting types from one to another when necessary.
PHP variables are Perl-like.

Valid and Invalid variables
	<?php  
	 $abc = 'Welcome';  //valid  
	 $Abc = 'W3resource.com'; //valid  
	 $9xyz = 'Hello world';  //invalid; starts with a number  
	 $_xyz = 'Hello world';  //valid; starts with an underscore  
	 $_9xyz = 'Hello world';  //valid  
	 $aäa = 'Hello world';  //valid; 'ä' is (Extended) ASCII 228.  
	?>

 

2.Output the Variables using Echo function.

The PHP echo statement is often used to output data to the screen. The example will show how to output text and a variable.

Echo Function
      <?php
        $txt = "Lauyou";
        echo "I love $txt!";
      ?>

 

3.Data Types in PHP

 

PHP Data TypesDescription
IntegersWhole numbers, without a decimal point
DoublesFloating-point numbers.
BooleansHave only two possible values either true or false.
NULLA special type that only has one value: NULL.
StringsSequences of characters
ArraysNamed and indexed collections of other values
ObjectsInstances of programmer-defined classes, which can package up both other kinds of values and functions that are specific to the class.
ResourcesSpecial variables that hold references to resources external to PHP

Anwar Yakkiparamban

Anwar Yakkiparamban is the founder of Lauyou Learning. Prior to Lauyou learning, Anwar worked at ARD Engineering & Development, Qatar. He holds bachelor degree in Electronics and Communication Engineering from Govt. Engineering College Idukki.

You may also like...