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.
1 2 3 4 5 6 7 8 9 |
<?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.
1 2 3 4 5 6 7 8 9 10 11 12 |
<?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.
1 2 3 4 5 6 7 8 |
<?php $txt = "Lauyou"; echo "I love $txt!"; ?> |
3.Data Types in PHP
PHP Data Types | Description |
---|---|
Integers | Whole numbers, without a decimal point |
Doubles | Floating-point numbers. |
Booleans | Have only two possible values either true or false. |
NULL | A special type that only has one value: NULL. |
Strings | Sequences of characters |
Arrays | Named and indexed collections of other values |
Objects | Instances of programmer-defined classes, which can package up both other kinds of values and functions that are specific to the class. |
Resources | Special variables that hold references to resources external to PHP |