PHP Variables


---
PHP Variables

1) Creating (Declaring) PHP Variables

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

2) Output Variables

<?php
$txt = "W3Schools.com";
echo "I love $txt!";
?>
<?php
$txt = "W3Schools.com";
echo "I love " . $txt . "!";
?>
<?php
$x = 5;
$y = 4;
echo $x + $y;
?>

3) Global and Local Scope

<?php
$x = 5; // global scope
function myTest() {
        // using x inside this function will generate an error
        echo "<p>Variable x inside function is: $x</p>";
}
myTest();
echo "<p>Variable x outside function is: $x</p>";
?>
<?php
function myTest() {
        $x = 5; // local scope
        echo "<p>Variable x inside function is: $x</p>";
}
myTest();
// using x outside the function will generate an error
echo "<p>Variable x outside function is: $x</p>";
?>
<?php
$x = 5;
$y = 10;
function myTest() {
        global $x, $y;
    $y = $x + $y;
}
myTest();
echo $y; // outputs 15
?>
<?php
$x = 5;
$y = 10;
function myTest() {
    $GLOBALS['y'] = $GLOBALS['x'] + $GLOBALS['y'];
}
myTest();
echo $y; // outputs 15
?>
<?php
function myTest() {
        static $x = 0;
        echo $x;
        $x++;
}
myTest();
myTest();
myTest();
?>

---

Post a Comment

0 Comments