Culpritz's Blog

Computer, Networking, Programming, Photoshop articles, tips & more.

HelloWorld in PHP in 15 different ways

Here i am demonstrating “traditional HelloWorld” examples in PHP using different ways like function, objects etc.. This will be helpful for beginners to understand the basic concepts of PHP used in practicle life.

    Basic

  1. Simple HelloWorld using echo()
        echo "HelloWorld";    // echo() is same as print()
    
  2. Using short open tags (if enabled in php.ini)
    <?= "Hello World" ?>
    
  3. Using Variable
        $var = "HelloWorld";
        echo $var;
    
  4. Using Arrays

  5. Using numeric arrays
        $array = array("HelloWorld");
        echo $array[0];
    
  6. Using associative arrays
        $array = array("greeting"=>"HelloWorld");
        echo $array["greeting"];
    
  7. Using multidimensional arrays
        $array = array("subarray"=>array("HelloWorld"));
        echo $array["subarray"][0];
    
  8. Using Functions

  9. By calling a function
        // definition
        function sayHello() {
            echo "HelloWorld";
        }
        // call
        sayHello();
    
  10. Passing argument by value to a function
        // definition
        function sayHello($arg) {
            echo $arg;
        }
        // call
        $var = "HelloWorld";
        sayHello($var);
    
  11. Passing argument by reference to a function
        // definition
        function sayHello(&$arg) {
            $arg = "HelloWorld";
        }
        // call
        $var = "";
        sayHello($var);
        echo $var;
    
  12. Using returned value from function

        // definition
        function sayHello() {
            return "HelloWorld";
        }
        // call
        echo sayHello();
    
  13. Using Objects

  14. Using constructor of “Greet” class
        // class
        class Greet
        {
            public function __construct() {
                echo "HelloWorld";
            }
        }
        // initiate object
        $obj = new Greet();
    
  15. Using destruction method of “Greet” class
        // class
        class Greet
        {
            public function __destruct() {
                echo "HelloWorld";
            }
        }
        // initiate then destroy object
        $obj = new Greet();
        unset($obj);    // cause print
    
  16. Using public method & private property of “Greet” class
        // class
        class Greet
        {
            private $var = "HelloWorld";
    
            public function sayHello() {
                echo $this->var ;
            }
        }
        // initiate object then call method
        $obj = new Greet();
        $obj->sayHello();
    
  17. Using static method & static property of “Greet” class
        // class
        class Greet
        {
            private static $var = "HelloWorld";
    
            public static function sayHello() {
                echo self::$var ;
            }
        }
        // call satic method without initiating class object
        Greet::sayHello();
    
  18. Using children class’s method to call its parent class’s protected propertry
        // our parent class
        class Greet
        {
            protected $var = "HelloWorld";
        }
    
        // our child class
        class Do_Greet extends Greet
        {
            public function sayHello() {
                echo $this->var ;    // accessing parent property (if private it will cause error)
            }
        }
    
        // we have to initiate children class object
        $obj = new Do_Greet();
        $obj->sayHello();
    
  19. Using children class’s static method to call its parent class’s protected static propertry
        // our parent class
        class Greet
        {
            protected static $var = "HelloWorld";
        }
    
        // our child class
        class Do_Greet extends Greet
        {
            public static function sayHello() {
                echo parent::$var ;    // accessing parent static property (if private it will cause error)
            }
        }
    
        // without initiating class call its static method
        Do_Greet::sayHello();
    

There are many other ways too but i’ve listed some common ones used in PHP. Feel free to share them!

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.