Chapter I - PHP Syntax
Well this is the first part of my PHP tutorials, which is all about basic syntax of PHP. In this thread you will be able to learn basic PHP syntax, variables, data types and constants. Alright, lets get this started.
1. Getting started with PHP:
If you have basic knowledge, you should understand the concept of tags. PHP tags enclose lines of PHP codes, you cannot write your PHP codes outside of tags. The start and end tags for PHP is shown below:
PHP:
<?PHP
// your codes here
?>
PHP:
<?
// your codes here
?>
PHP:
<script language = "php">
// your codes here
</script>
To display a message on the screen, we can use the functions print or echo. Strictly speaking, these are not the type of functions we normally refer to, since there is no need to enclose anything inside parenthesis(). An example is shown below:
PHP:
print "hello world";
echo "What a beautiful day!";
At times it is rather necessary to write comments so that your codes make sense to you even if you have taken a long break from PHP programming. It will also help other programmers read your script files. To write comments, simply use double slashes in front of your sentences:
PHP:
// This is a comment
3. Variables:
A variable is a type of container, which holds one particular value that can be of any data type. You can only assign one value to a variable, and the value can be changed at any time. To create a PHP variable, use the syntax:
PHP:
$var = value;
PHP:
$num = 10;
$name = "Sarah";
$pi = 3.14159265;
Another very important thing about variables is that its name can only contain letters, numbers or underscores. A variable name beginning with a number is not valid, nor is a variable that contains spaces. Examples of valid variable names are shown below:
PHP:
$name
$w75t
$_t013yuf
PHP:
$8543pod
$re+arb
$my food
4. Data Types:
Data types are not that important in PHP compared to C++ and Ruby, but it is still important to play around with them. There are six basic data types, namely:
integer: 8, 99, 100000
double: 1.5, 2.6666, 3.14
boolean: true and false
string: "Anna", "Hello World".
array
object
null
resource
It may be necessary to see what data type a variable is currently holding. To do this, use the function below:
PHP:
gettype($var)
PHP:
settype($var, datatype)
PHP:
$num = 3.1415926;
settype($num, int);
echo $num;
You may also use PHP's built-in functions is_datatype($var) to test if a variable contains the desired datatype. This types of functions return true if the variable contains the same datatype, or it returns false. They can be easily integrated with if...else statements.
A list of this type of functions is shown below:
PHP:
is_int($var)
is_double($var)
is_string($var)
is_bool($var)
is_array($var
is_object($var)
is_null($var)
is_resource($var)
Constants, in contrast to variables, are containers storing values that cannot be changed later in your codes. To create a PHP constant, use the following syntax below:
PHP:
define("Constant's name", value);
PHP:
define("NAME", "Richard");
echo NAME;
Alright, this is all for basic syntax. The next tutorial will introduce the concept of operators and how they work in PHP. This is my first time writing tutorials for PHP programming, lemme know if you have any suggestions on this. Thanks for reading this thread everyone, and have fun with Mysidia Adoptables script files. ^^