PHP Coding

These files have a .php file extension.

You just wrap your lines of PHP in <?php and ?>. The interpreter will process only those parts of the text as PHP, replacing them with the output of your code. Here’s a quick example. Open up your text editor of choice and put in this text:

<h1> Hello PHP! </h1>
<?php
   echo "<p>I'm getting good with PHP.</p>";
?>
Demo of above code

Hello PHP!

I'm getting good with PHP.

Variables

A variable is a storage place; it’s something you can come back to, to get the same value again. Here’s an example:

$message = "<p>I'm getting good with PHP.</p>";
$an_array = array("HTML", "CSS", "JavaScript", "PHP");
$number = 1232.22;

Functions

Adding to the fundamentals of PHP, it is important to take a look at functions. Functions provide a way to perform a set of scripted behaviors now, or save them for later, and depending on the function they may even accept different arguments.

A function is defined by using the function keyword followed by the function name, a list of commas separated arguments wrapped in parentheses, if necessary, and then the PHP statement, or statements, that defines the function enclosed in curly braces, {}.

function say_hi ($name) {
return "Hello, $name";
}

Control Structures

PHP follows the syntax of popular C language. All the control structures are same as in C.

$name = "Transpire";
if ($name == "Transpire") {
   echo "Hello, Transpire!";
} else {
   echo "Hi, Welcome to Transpire Solutions! What's your name?";
}

$friends = array("Watson", "Mycroft", "Sherlock", "Lestrade", "Andrew");
for ($i = 0; $i < 4; $i++) {
   echo "<li> $friends[$i] </li>";
}

$friend = "Mycroft";
switch ($friend) {
   case "Watson":
      echo "Hello, Watson.";
      break;
   case "Mycroft":
      echo "'sup, Mycroft?";
      break;
   case "Sherlock":
      echo "Good day, Sherlock!";
      break;
   case "Lestrade":
      echo "How are you, Lestrade?";
      break;
   default:
      echo "Hi, I'm Andrew! What's your name?";
}

comments powered by Disqus