PHP - examples - 01 : syntax - variables - constants

Revision:


syntax - basics

echo statement

My first PHP script!
Hello World
Hi, there
Hope you are doing fine

PHP syntax

PHP syntax Hello
code
            <div>
                <?php 
                    echo "My first PHP script!"."<br>";
                    echo "Hello World"."<br>";
                    ECHO "Hi, there"."<br>";
                    eCHo "Hope you are doing fine";
                ?>
                <h4><?php echo "PHP syntax" ?></h4>
                <?php 
                    echo "PHP syntax";
                ?>   
                <?php 
                    $message = "Hello";
                    echo $message;
                ?>
            </div>
        

basics

hello world
fine to get the opportunity

my car is red
my car is
my car is

hi there, my world!
5
6

I love playing soccer!
codes:
            <div>     
                <?php 
                    echo "hello world"."<br>";
                    print "fine to get the opportunity"."<br>";
                    $color = "red";
                    echo "<br>";
                    echo "my car is ".$color."<br>";
                    echo "my car is ".$COLOR."<br>";
                    echo "my car is ".$coLOR."<br>";
                    echo "<br>";
                    $txt = "hi there, my world!";
                    $x = 5;
                    $y = 6;
                    echo $txt; 
                    echo "<br>";
                    echo $x;
                    echo "<br>";
                    echo $y; 
                    echo "<br><br>";
                    $txt = "playing soccer";
                    echo "I love $txt!";
                ?>
            </div>
        

variables

 $variable_name = value;
My car is red
My house is green
My boat is white
PHP is awesome
Hello world!
5
10.5

I love my website!
I love my website!
9

PHP is fun!

code:
            <div>
                <?php 
                    $color = "red";
                    $COLOR = "green";
                    $coLOR = "white";    
                    echo "My car is " . $color . "< br>";
                    echo "My house is " . $COLOR . "< br>";
                    echo "My boat is " . $coLOR . "< br>";
                    $title = "PHP is awesome";
                    echo $title;
                ?>

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

                <?php
                    $txt = "my website";
                    echo "I love $txt!"."< br>";
                    echo "I love " . $txt . "!"."< br>";
                    $x = 5;
                    $y = 4;
                    echo $x + $y;
                ?>
                <?php
                    $title = "PHP is fun!";
                ?>
                <h4><?=$title?></h4>
            </div>
        

variables scope

Variable xx inside function is:

Variable xx outside function is: 5


Variable yy inside function is: 5

Variable yy outside function is:


15
            <div>
                <?php
                    $xx = 5; // global scope
                    function myTest() {
                    // using x inside this function will generate an error
                    echo "< p>Variable xx inside function is: $xx< /p>";// use of unassigned variable "$xx";
                    }
                    myTest();
                    echo "< p>Variable xx outside function is: $xx< /p>";
                ?>

                <?php
                    function myTest_a() {
                    $yy = 5; // local scope
                    echo "< p>Variable yy inside function is: $yy< /p>";
                    }
                    myTest_a();
                    // using yy outside the function will generate an error
                    echo "< p>Variable yy outside function is: $yy< /p>";
                ?>
          
                <?php
                    $x_x = 5;
                    $y_y = 10;
                    function myTest_b() {
                    global $x_x, $y_y;
                    $y_y = $x_x + $y_y;
                    }
                    myTest_b();
                    echo $y_y; // outputs 15                       
                ?>
            </div>
        

constants

A constant is simply a name that holds a single value. The value of a constant cannot be changed during the execution of the PHP script.

To define a constant, you use the define() function, which takes the constant,s name as the first argument and the constant value as the second argument.

100vw
Array
Welcome to my website!