All posts in Tutorials

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 %s\n&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 %s\n&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

Writing Functions In PHP – Default Values

Writing Functions in PHP – Introduction

Writing Functions in PHP – Flexibility

Writing Functions in PHP – Return Values

Writing Functions in PHP – Global Variables

We saw earlier how the numbers of arguments that were passed into a function needed to match the number of arguments that the function was defined with. But with default arguments, that’s not entirely true because we can set some defaults so that if it doesn’t get a value, it has something that it can assume, and this not only will make our functions more flexible but also error resistant. Let’s go ahead and try one now…

<html>
<head>
<title>Writing Functions In PHP - Default Values</title>
</head>
<body>
    <?php

        function paint($color) {
            echo &quot;The color of the room is {$color}.&quot;;
        }

        paint(&quot;blue&quot;);

    ?>
</body>
</html>

Output: the color of the room is blue.

That is a simple straight forward example. What if we didn’t pass a value into the function?

    <?php

        function paint($color) {
            echo &quot;The color of the room is {$color}.&quot;;
        }

        paint();

    ?>

Sure we will get an error message.

Output:
Warning: Missing argument 1 for paint()…
Notice: Undefined variable…
The color of the room is .

Functions With Default Values

In the example above, we can set a default value for $color, so that if it doesn’t get a $color, we have something to fall back on. We do that by simply putting in a normal equal assignment in the function right after the arguments.

    <?php

        function paint($color=&quot;red&quot;) {
            echo &quot;The color of the room is {$color}.&quot;;
        }

        paint();

    ?>

So now, “red” will be our default value if we haven’t passed any value to the function. In other words, if no value were passed into the function, use “red”! The default value is overwritten when you specify a value, otherwise the value will stay the same. Even though we didn’t ask for another argument, it went ahead and assume “red”. If instead we tell it “blue”, it goes ahead and uses “blue”.

So you can see how this is going to make our function a little more error resistant because it will not give us that big nasty error if we didn’t pass something in.

Flexible Functions With Default Values

Let’s go ahead and make this function even more flexible. Lets say we just are not going to paint the “room”, but instead we are going to paint the $room as a variable.

<html>
<head>
<title>Writing Functions In PHP - Default Values</title>
</head>
<body>
    <?php

        function paint($room=&quot;office&quot;,$color=&quot;red&quot;) {
            echo &quot;The color of the {$room} is {$color}.&quot;;
        }

        paint(&quot;bedroom&quot;,&quot;blue&quot;);

    ?>
</body>
</html>

Output: The color of the bedroom is blue.

So, this function takes two arguments into it and we must pass two values into it as well. Even though we didn’t pass anything into the function, it went ahead and made the assumption with the default value that we have declared earlier. All of this will work:

<?php paint(); ?>

Output: The color of the office is red.

<?php paint(&quot;bedroom&quot;); ?>

Output: The color of the bedroom is red.

What if we say “blue” and leave out the other one which is the $room?

<?php paint(&quot;blue&quot;); ?>

Output: The color of the blue is red.

It went ahead and says “The color of the blue is red.”, and why is that? This is because it’s going to still assume that the arguments are coming in the correct order. So we have to make sure that we always pass something in the right order.

Required Value vs Not Required value

Consider this example:

    <?php

        function paint($room,$color=&quot;red&quot;) {
            echo &quot;The color of the {$room} is {$color}.&quot;;
        }

        paint(&quot;bedroom&quot;,&quot;blue&quot;);

    ?>

Output: The color of the bedroom is blue.

We know this will work. $room is a required value and $color is not a required value, in this function. This means, a required value is a value that is having the highest priority for us, we want to make sure that we pass the correct value to this required value. Not like the $color value, which is not a required value, we can choose whether we want to specify the value or just let the function go ahead and assume for us.

If we did it the other way around and made our $color as the required value and pass in only one argument into the function:

    <?php

        function paint($room=&quot;office&quot;,$color) {
            echo &quot;The color of the {$room} is {$color}.&quot;;
        }

        paint(&quot;bedroom&quot;);

    ?>

Output:
Warning: Missing argument 2 for paint3()…
Notice: Undefined variable…
The color of the bedroom is.

This function will take “bedroom” as being the $room, and it’s going to ask for the required value which is the $color. We didn’t pass any value to the function for the variable $color. This will return an error message to us.

So you want to always make sure that your default values occur last in line. In other words, anything that is required needs to come first in the arguments list, anything that is optional needs to come later and the order of this doesn’t matter and you can’t skip spaces in between. So you need to pass in something along the way.

Conclusion

Another thing that I found helpful with default arguments is that you very quickly get a sense of what kind of information you’re expecting to be passed in to your function.

<?php function paint($color=&quot;red&quot;,$room=&quot;office&quot;) {?>

Clearly here, I am looking for a color, I know it is going to be “red” and I am looking for a room which I know is something like an “office”. This gives us some context for knowing what those variables actually mean in our function.

Now that we’ve looked into default argument values, I think we’ve explored enough of functions that we will be able to get a lot out of them once we start coding and developing.

Thank you and enjoy learning.

Creating A Tilt-Shift Effect Using Photoshop

To get the best effect, it’s advisable to use an image that is looking down on your subject. An image such as a busy street scene viewed from a multi-story building or where the camera is above the horizon line, works perfectly. Alternatively, you can also download the image I’ve used in this tutorial.

We’ll start off by increasing the color saturation. This makes the effect more noticeable.

After opening your image, create a new Vibrance Adjustment Layer. You can do this by selecting the half-filled circle at the bottom of your Layers palette or from the Layer > New Adjustment Layer > Vibrance… menu option. The amount you increase the Vibrance & Saturation will depend on your particular image. For this image though, I’ve increased both to +40 to really exaggerate the colors.

First image example

Next, add a new Curves adjustment layer. Again, you can select this from the half-filled circle at the bottom of the Layers palette or from the Layer > New Adjustment Layer > Curves… menu option. Increase the highlights and decrease the shadows slightly to provide further contrast between the colors. The amount of adjustment required in the curves dialog may vary depending on the particular image used.

second image example of tutorial

The main effect in a Tilt-Shift image is the shallow depth of field. To simulate this, we first need to blur the image. Select your background layer (or the layer that has your image) and press Ctrl-J (Command-J on Mac) to duplicate this layer. If it’s not already, select this new duplicated layer and then select Filter > Blur > Gaussian blur… menu option. Blur your image with a Radius of 4.5 pixels.
Third image example of tutorial

With your blurred layer still selected, add a new Layer Mask by selecting the Add Layer Mask icon at the bottom of the Layers palette or by selecting the Layer > Layer Mask > Reveal All menu option. Make sure your Layer mask (not the image) is selected by clicking on it. Select the Gradient Tool and in the options palette, select Reflected Gradient (4th Gradient icon from the left). Also, ensure that the ‘Reverse’ checkbox is unticked. Next, drag the gradient vertically over a small portion of the image. To make sure it’s perfectly vertical, hold down the Shift key while you drag. What you are doing here is creating a mask on the blurred image so that the original image sitting on the layer below, can be seen. Since we only want to simulate a narrow Depth of Field, the gradient should be quite small. It may take a couple of goes to get the correct effect. Once you’ve added your gradient to the Layer mask, you should find that the area you dragged the cursor over will no longer be blurred.
fourth image example for tutorial

To ‘fine tune’ your gradient, click the small “link” icon between the image and the Layer mask (in the Layers palette) to turn it off. Making sure your Layer mask is still selected, select the Move Tool and use the Up & Down arrow keys to fine tune the position of your mask. The subject matter of your image will influence where best to position the mask to get the best effect.
5th image example for tutorial

Once you’re happy with the placement of your mask, your image is complete. You can now sit back and contemplate what you’re going to “miniaturise” next ;-)