syntax, variables, comments and functions

Posted in PHP by

Get started using php, learn the syntax, create variables and program your own functions.

Every script (it is called, script, because it is interpreted) can contain php and html code. It is possible to mix both, however it is best to keep em separated. The php opening tag & must be used, before any php code. You can close the tag with ?>. It is possible and recommend to leave away the closing php tag, if there is no html code below.

<php
echo 'the #care is awesome';
This will output "the #care is awesome". The syntax is pretty easy to pickup, every statement (i.e.: echo...) ends with a semicolon. Every string is between quotes, you can use apostrophes or double quotes. It does not matter, however if you use apostrophes, which we recommend, variables inside the quotes won't be interpreted.
<php
$url = 'http://careout.com';
$path = 'php';
//This is a single-line comment.
echo $url.'/'.$path;
Variables in php are not bound to a fixed type and php will take care of em. They are created with a dollar sign before a unique name. It is possible to put them together using a dot. The example will output "http://careout.com/php". Using two slashes starts a comment, which is not interpreted, till the end of the line. From now on most things are explained in the code, cause you can copy and paste it.

There are a huge number of integrated functions, you will learn a small, but important subset of em in our php articles. You can always loop them up in the documentation. Creating your own functions is a benefit, because they can be used over and over again.

<php
/ This is a multi line comment.
Do not forget to close it.
Every function begins with the function keyword,
afterwards a unique name is needed,
and between ($are,$parameters,$separated_by,$commas),
the body is within { these }./
function shorten($input){
  //Uses the internal function substr.
  //Any open brace must always be closed, otherwise you got an error.
  return substr($input,0,3);
}

In the next article, you will learn about operators, control structures and arrays.

Published at , Updated at 2011-10-25

next: Debugging PHP with xdebug and notepad++ prev: PHP Development Environment