PHP Variable

Last Updated: February 4, 2022

Variables in PHP are used to hold values of string, numbers, array, objects, or any other data types found in PHP

Syntax

$varible_name =  value


When you define variables you have to follow the following rules

  • Variable name should be started with $ sign
  • Second  character can be (a-z),(A-Z) or the underscore ( _ )
  • The remaining characters can be underscores, letters, or numbers
  • Variables are case sensitive in PHP
<?php
$my_name = “I am Tim”

Use echo  command to show the value of the variable in your PHP code

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>PHP Variables</title>
</head>
<body>
    <?php
    $my_name = “I am Tim”

    ?>
    <h1><?php echo $my_name; ?></h1>
</body>
</html>

You can use the following syntax too

<?= $my_name ?>

What is the output of this ?

<?php
$var1 = “x”;
$x = “X”

echo $$var1;

Answer is X

Test