Elements of PHP (Part 2)

If you missed the first part of this tutorial visit “Elements of PHP” where we learned some basic fundamentals in getting started with php.

Elements of PHP (Part 2)

Let’s continue with our tutorial on PHP’s fundamentals. First, focus on punctuation.

Dot your T’s, cross your I’s

Notice the punctuation characters used in the example script from the first half of this tutorial, especially the semicolon that terminates each line. PHP needs this character to occur at the end of each statement, to separate it from the previous statement. This requirement might take some getting used to if you’ve done a lot of coding in JavaScript, whose terminating semicolons are optional.

Get aware of PHP’s other guidelines and rules regarding punctuation, including this tip about quotation marks: you can surround strings with single or double quotation marks, but don’t mix and match the two. If you start a string with a double quote, end the string with double quotes.

Note that you can embed strings within strings in PHP. Try this example to see an illustration of this fact:

<pre><?php
echo &quot;In the forest is my mate 'Doris' ...&quot;;
echo 'who reads &quot;The London Times&quot;';
?>

In this example, we enabled display of single quotes by enclosing them in double quotes, and vice-versa.

You can also use the chr function to display embedded quotes (or any other ASCII character):

echo “In the forest is my mate “.chr(39).”Doris”.chr(39);

But this approach isn’t nearly as clear, or as easy to write.

About variables

If you’ve only coded in C, C++, Java, or a similar, “strongly-typed” language, be careful that you don’t let your guard slip when you start coding in PHP: you don’t have to declare variables in this language.

All you need to create a variable in PHP is to start using it–assign a value to a variable, specifically. That assignment can be something as simple as this:


$x = "some string";

Or,

$x =13;

Regardless of what you assign to the variable–integer, float, string, object–the PHP interpreter on your server will know how to evaluate the variable’s correctly. In other words, if you assign a number to a variable, PHP will understand that you can involve that variable in subsequent calculations and formulas. If you assign some text to a variable, PHP will know that you can run functions like strcmp (string compare) or stripos (find a character in a string) on it.

But, regardless of whether your variables are numeric, text or some other type, do preface all variables with the $ character. Without this character, the PHP interpreter won’t see your variables.

Read more about PHP variables here.

Output statements

We’ve so far used just the echo function to create output from PHP scripts. But, PHP has many output functions besides that statement.

Open up one of the foregoing scripts and replace the “echo” keywords with “print.” Re-run the script and notice that the output is the same: “print” can be used in place of “echo.”

Try another output function, which offers options to format the output:

<pre><?php
printf ( &quot;In the forest is my mate %sn&quot;, &quot;Doris&quot;);
?>
[/p]
</pre>
The printf function in the mini script just given should look very familiar to you, if you're used to C programming. This function lets you use characters, like the &quot;%s&quot; in this example, to finely format the content being output. The &quot;%s&quot; specifier is for displaying strings, &quot;%d&quot; is for integers, and &quot;%f&quot; is for floating-point numbers. You can use many other format specifiers, including those to print octal and hexadecimal numbers. <a href=&quot;http://us2.php.net/manual/en/function.sprintf.php&quot;>See this page on the main PHP documentation site for details</a>.
<h3><strong>Case sensitivity</strong></h3>
Make the following change in one of the scripts covered so far: change the case of an &quot;echo&quot; statement from lower to upper case: &quot;ECHO.&quot; Notice that your IDE doesn't complain when you do this, and the server will run the script and display its content as it did originally. PHP seems to be insensitive to case.

Now, change the script to read as follows:

1
<pre><?php
$someword = &quot;Doris&quot;;
$SOMEWORD = &quot;Lavoris&quot;;
PRINTF ( &quot;In the forest is my mate %sn&quot;, $someword);
?>

When you run this script, you don’t see “Lavoris” being output, but “Doris.” The upper case $SOMEWORD is a different variable from the lower case one: PHP is sensitive to case, when it comes to variable names.

PHP pretending to be HTML

One of PHP’s many abilities is displaying content from related scripting languages — especially HTML and JavaScript. The following PHP script is an example of this ability. Enter this program code in your IDE and upload it to your server. Then, request the page in your browser.

<pre><?PHP

echo '

<!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.01 Transitional//EN&quot;>
<html>

    <head>
        <title>
        </title>
        <script type=&quot;text/javascript&quot;>
            onload = inish;

            function inish() {
                document.getElementById(&quot;p1&quot;).onmouseover = mouse_over_handler;
            }

            function mouse_over_handler() {
                alert(&quot;Greetings from PHP.&quot;);
            }
        </script>
        <meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=UTF-8&quot;>
        <style type=&quot;text/css&quot;>
            .style1 { font-style: italic; text-decoration: overline underline; font-family:
            Verdana; }
        </style>
    </head>

    <body>
        <P id=&quot;p1&quot; class=&quot;style1&quot;>
            Mouse over Here.
        </P>
    </body>
</html>';
?>

References

PHP Documentation Home Page

One Comment on "Elements of PHP (Part 2)"

  1. Jackson says:

    Have you ever been fooled by images that look realistic but are actually Photoshopped? You need some image software to help you detect them. But such kind of software is usually pretty expensive. Instead, some online fake detection tool can help on this for free. Like Photoshopped Image Killer. PSKiller extracts JPEG quantization tables from your image and compares them with those in database. If they match tables of Photoshop, image will be classifier as Photoshopped. Exif tags of JPEG are utilized as well.

Got something to say? Go for it!