PHP: String operators

Earlier, we have used .(dot) to join 2 strings together.

<?php

$text1 = 'Hello ';
$text2 = $text1 . 'World!'; 

echo "$text2";
echo "\n";

$text3 = 'Upwhere We ';
$text3 .= 'Belong!';  

echo "$text3";
echo "\n";

$num = 3;

echo "Result: " . ($num + 3); 

?>

In the statement $text3 .= ‘Belong!’, the .(dot) here is a unary string operator.

The statement here will add ‘Belong!’ to variable $text3 itself.

PHP: Variables are case sensitive

In PHP variables declared are case sensitive.

<?php
$myText = 'I love programming ';
$mytext = 'the most liberal programming language in the world PHP.';

echo "$myText  $mytext";
?>
Try The Code

$myText is not the same as $mytext in PHP.

Note also variable string can be declared in single quote such as ‘I love programming’. We have put 2 variables in the echo statement.

If you changed echo statement into using single quotes, it will not work in this case.  Try to stick to using double quotes in echo statement.

PHP: Variables

Sit back and relax.

Let’s not hurry and move to the more complex example.  Sometimes, it is good to just run code written by others.

We now look at how variables are declared in PHP.

<?php

$myText = "Happy New Year! "; 
$myNum = 2014;

echo $myText;
 
echo "The year is ". $myNum;
?>
Try The Code

In PHP, a variable is preceded by a $ sign and declaration is actually not needed.

As in the example above, if you have $myNum = 2014, the PHP parser will interprete $myNum as an integer directly.

The $myText is a string variable as the text Happy New Year! is enclosed in a double quote.

Notice in line 8, there is a .(dot) in the line.  The . in PHP is a string concatenation. It joins a string and an integer or a string to a string together.

PHP: Our first PHP script

We will first look at a few PHP examples.  Here I assume that you have some understanding of HTML which is sufficed for us for now.

<html>
 <head>
  <title>PHP Test</title>
 </head>
 <body>
 <?php echo '<p>Hello World</p>'; ?> 
 </body>
</html>
Try The Code

Echo is similar to writeln in C.

In this example, the entire string <p>Hello World</p> will be output to the browser and this string is also what the browser will see.

And as for us, we will see Hello World in the browser when the script is ran.

PHP: The language

PHP is a popular general-purpose scripting language that is especially suited to web development.

Fast, flexible and pragmatic, PHP powers everything from your blog, as in this wordpress blog, to the most popular websites in the world.

When it comes to developing for the web, you could say PHP provides everything for us to build a complete web site.  And it does certain things really well such as in accessing the backend database.

If you have gone through the Javascript tutorial, you would find that Javascript gives a better separation between HTML and script whereas for PHP, the HTML is embedded in the script making it more difficult to learn.

If you are at this point and wanted to learn PHP, I would suggest that you also brush up on database and have some basic understanding of database tables, querying using select statements, insert and deleting records in the database table.

I will create a MySQL tutorial at a later time.