PHP Four Ways

See: http://php.net/manual/en/langref.php

<!DOCTYPE html>

<html>

<head>

   <meta charset="utf-8" />

   <title>PHP 01a</title>

</head>

<body>

   <p>

      <?php echo 'Hello, World!'; ?>

   </p>

</body>

</html>

 

<?php and ?>

·         PHP opening and closing tags

·         Tells the PHP interpreter where to begin and end parsing

·         If the file is only PHP code, omit the closing tag.  (Prevents accidental whitespace being added after the PHP closing tag, which may cause unexpected effects.)

echo

·         A language construct

·         Outputs one or more strings

'Hello, World!'

·         Single quoted string literal

·         All instances of \ are treated as a literal backslash except \’, which escapes the single quote

;

·         Instruction separation

·         Each statement is required to be terminated with a semicolon

·         The closing tag implies a semicolon


 

<!DOCTYPE html>

<html>

<head>

   <meta charset="utf-8" />

   <title>PHP 01b</title>

</head>

<body>

   <p>

      <?php

         require 'hello.php';

         echo hello_world();

      ?>

   </p>

</body>

</html>

hello.php

<?php

function hello_world()

{

    return "Hello, World!";

}

 

require

·         Includes and evaluates the specified file

·         Upon failure it stops the script from running

hello_world()

·         Calling the hello_world function

function hello_world()

{

}

·         Defines a function named hello_world

·         Function names start with a letter or underscore, followed by any number of letters, numbers, or underscores

return

·         Immediately ends execution of the current function

·         Returns the specified value

"Hello, World!"

·         Double quoted string literals

·         \ is treated as starting an escape sequence


 

<!DOCTYPE html>

<html>

<head>

   <meta charset="utf-8" />

   <title>PHP 01c</title>

</head>

<body>

   <p>

      <?php

      require 'Hello.php';

      echo Hello::world();

      ?>

   </p>

</body>

</html>

Hello.php

<?php

 

class Hello

{

    public static function world(){

        return "Hello, World!";

    }

}

 

Hello::world()

·         Calls a static public class method

class Hello

{

}

·         Defines a class

·         Class names start with a letter or underscore, followed by any number of letters, numbers, or underscores

public static function world(){

}

·         Defines a static public class method

·         Method names start with a letter or underscore, followed by any number of letters, numbers, or underscores


 

<!DOCTYPE html>

<html>

<head>

   <meta charset="utf-8" />

   <title>PHP 01d</title>

</head>

<body>

   <p>

      <?php

      require 'Hello.php';

      $h = new Hello();

      echo $h->helloWorld();

      ?>

   </p>

</body>

</html>

Hello.php

<?php

 

class Hello

{

    public function helloWorld(){

        return "Hello, World!";

    }

}

 

$h

·         A PHP variable

·         Must begin with the $

·         A valid variable name starts with a letter or underscore, followed by any number of letters, numbers, or underscores.

$h = new Hello()

·         Creates a new instance of a class

$h->helloWorld()

·         Calls an instance method

public function world(){

}

·         Defines a public method of the class