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
- Simple HelloWorld using echo()
echo "HelloWorld"; // echo() is same as print()
- Using short open tags (if enabled in php.ini)
<?= "Hello World" ?>
- Using Variable
$var = "HelloWorld";
echo $var;
Using Arrays
- Using numeric arrays
$array = array("HelloWorld");
echo $array[0];
- Using associative arrays
$array = array("greeting"=>"HelloWorld");
echo $array["greeting"];
- Using multidimensional arrays
$array = array("subarray"=>array("HelloWorld"));
echo $array["subarray"][0];
Using Functions
- By calling a function
// definition
function sayHello() {
echo "HelloWorld";
}
// call
sayHello();
- Passing argument by value to a function
// definition
function sayHello($arg) {
echo $arg;
}
// call
$var = "HelloWorld";
sayHello($var);
- Passing argument by reference to a function
// definition
function sayHello(&$arg) {
$arg = "HelloWorld";
}
// call
$var = "";
sayHello($var);
echo $var;
-
Using returned value from function
// definition
function sayHello() {
return "HelloWorld";
}
// call
echo sayHello();
Using Objects
- Using constructor of “Greet” class
// class
class Greet
{
public function __construct() {
echo "HelloWorld";
}
}
// initiate object
$obj = new Greet();
- 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
- 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();
- 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();
- 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();
- 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
Like this:
One blogger likes this post.