Learn PHP


PHP Variable


A variable is a container holding a certain value.


Variable is store the value or information


Syntax :


Variables in PHP beginning with $ followed by a letter or an underscore, then any combination of letters, numbers, and the underscore character.


Here are the rules we would follow to name variables.


  • Variable names begin with a dollar sign ( $ )
  • The first character after the dollar sign must be a letter or an underscore
  • The remaining characters in the name may be letters, numbers, or underscores without a fixed limit

Define PHP variables:


We cannot start a variable with a number. A list of valid and invalid variable names is shown in the following table.


Variable Description
$name Right
$_name Right
$__name_ Right
$name1 Right
$1name Wrong ! Start with number
$name's Wrong ! No symbol other than "_" underscore are allowed

Variables are case-sensitive. $NAME is not the same variable as $name.


    <?php
        $txt = "Hello world!";
        $x = 50;
        $y = 15.4;

        echo $txt;
        echo "<br>";
        echo $x;
        echo "<br>";
        echo $y;
    ?>

Output:


Hello world!
50
15.4


Variable substitution


In PHP we can write variable name into a long string and PHP knows how to replace the variable with its value. Here is a script showing assigning and outputting data.


<?php
       $name = "studentsempire.com";
       print "Your name is $name\n";
       $name2 = $name;
       print 'Goodbye, $name2!\n';
?>

Output:


Your name is studentsempire.com
Goodbye, $name2!\n

PHP will not perform variable substitution inside single-quoted strings, and won't replace most escape characters.


In the following example, we can see that:


  • In double-quoted strings, PHP will replace $name with its value;
  • In a single-quoted string, PHP will output the text $name just like that.

<?php
        $food = "grapefruit";
        print "These ${food}s aren't ripe yet.";
        print "These {$food}s aren't ripe yet.";
?>

Output:


These fruits aren't ripe yet.These fruits aren't ripe yet.

php variable naming conventions


There are a few rules that you need to follow when choosing a name for your PHP variables.


  • PHP variables must start with a letter or underscore "_".
  • PHP variables may only be comprised of alpha-numeric characters and underscores. a-z, A-Z, 0-9, or _ .
  • Variables with more than one word should be separated with underscores. $my_variable
  • Variables with more than one word can also be distinguished with capitalization. $myVariable



PHP Tutorials


Home

Introduction

Syntax

Variable

Comment

Echo and Print Statement

Data Types

Boolean

Integer

Number Convert

PHP String

php variable substitution in string

php string element

php string function

php addcslashes function

php addslashes function

php get magic quotes gpc function

php get meta tags function

php urlencode function

PHP $ and $$ Variables

PHP constant

PHP Magic Constants

PHP Static Variable

PHP Super Globals

PHP Globals

PHP Server

PHP reserved variable get

PHP reserved variable post

PHP reserved variable FILES

PHP reserved variable Cookies

PHP reserved variable Session

PHP reserved variable Request

PHP reserved variable environment

PHP Operator

PHP Arithmetic Operator

PHP Comparison Operator

PHP Incrementing and Decrementing Operators

PHP Identical Operators

PHP Logical Operators

PHP String Operators

PHP Ternary Operators

PHP Execution Operators

PHP Operator Precedence

PHP Array Operator

PHP Class

PHP Class Inheritance

PHP constructor and destructor

PHP abstract class

PHP class access control

PHP statement

PHP If statement

PHP else statement

PHP else if statement

PHP control structure statement

PHP while loop

PHP do while loop

PHP for loop

PHP for each loop

PHP break statement

PHP continue statement

PHP declare statement

PHP return statement

PHP require statement

PHP include

PHP require once

PHP include once

PHP goto statement

PHP function

PHP function parameter

PHP function scope

PHP array

PHP array operator

PHP array element loop

PHP array foreach loop

PHP array multidimensional

PHP html forms

PHP get and post method

form redirect

file inclusion

file i/o