Absolute PHP Basics for WordPress Newbs

“I’m just a designer. I’m comfortable with CSS but PHP scares me.” Yeah ok, well first off, if you are a designer…. instead of banging your head into a wall to learn a coding language, why not hire a developer? Some of my best clients have been designers. It is kind of a win, win. I get to work on pretty sites and you get to do more designing and less stressing. Send me an email and let’s see if I can help.

But if you insist on trying to defy your right-brained designery nature and become more left-brained here are a couple of points that I think will help you as you try to learn both PHP and its sub-dialect of WordPress.

Function Function What’s Your Function

Functions do things. Ever learn about functions in middle school math class? Magic black-boxes where numbers go and come out as something else? Well it is pretty much the same. Let’s start with the simplest function possible, a nice spin off on the classic “hello world”.

function hello_bacon(){
  echo “hello bacon”;
}

hello_bacon(); // prints: hello bacon

But not all functions echo out a value because we don’t always want to echo (which is just another way to print something) a value out right away. Sometimes we need to process it more, etc. In that case you’d return a value. Returning a value is just the opposite of echoing a value. It says, don’t print it right now, save it for later.

function hello_bacon(){
   return “hello bacon”;
}

hello_bacon(); // prints: NOTHING! oh hold up!

To print it later we’d have to actually echo out the function.

echo hello_bacon();

or

$hello = hello_bacon();
echo $hello; // prints: hello bacon

See what we did there in that second one? We defined a variable, which if you’ll remember from math class is a bit of an amorphous blob that can have any value. Then we set that variable equal to the value returned from the hello_bacon() function.

Getting into Arguments

The hello_bacon() function is obviously super simple. It always returns the same thing. Well that’s not so much fun and sort of defeats the purpose of a function. Remember those magic black boxes? Well now we want to take a variable in, manipulate it and spit it back as something new and shiny. We send variables into functions as arguments to that function.

function favorite_food($food){
   echo 'My favorite food is ' . $food;
}

favorite_food(); //prints: My favorite food is <--didn't pass an argument so $food is empty, and I'm hungry.
favorite_food('guacamole'); //prints: My favorite food is guacamole

As you can see above, the favorite_food() function accepts 1 argument. Inside of the favorite_food() function this argument will be referred to by the variable $food. This sort of touches on a concept that we’ll get into in a bit, called variable scope. But for now remember that $food does not exist outside the favorite_food() function.

favorite_food('guacamole'); //still prints: My favorite food is guacamole
echo $food; //nothing doing

If you need to ensure that you never get an empty value for “My favorite food is ____” then you can give an argument a default. If you don’t pass an argument when calling a function it will simply use the default. Note below that ‘bacon’ is the default value of the $food variable.

function favorite_food($food = 'bacon'){
   echo 'My favorite food is ' . $food;
}

favorite_food(); //prints: My favorite food is bacon
favorite_food('guacamole'); //prints: My favorite food is guacamole

By now you might have noticed that we’ve only been dealing with what are called, ‘string’ variable types…. words or letters essentially. You’ve been seeing me combine strings together with a period (.) in what is called string concatenation. But just like math class, PHP functions work on numbers.

function double($x){
   return $x * 2;
}

$a = 10;
$a = double($a);
echo $a; //prints: 20

Variable Scope

Whoa hold up! You just said the argument was called $x, but there you go calling it $a. Yup. Remember when I said it was important to remember that variables (usually) only exist inside the function where they are defined. That is what is called variable scope. In PHP and WordPress you can declare variables as global in scope, meaning $x has the same value everywhere, but by default variables are local in scope, meaning again, they only exist within the functions using them.

I could rewrite the above to be:

function double($guacamole){
   return $guacamole * 2;
}

$bacon = 10;
$bacon = double($bacon);
echo $bacon; //prints: 20

I can call the variables anything I want, because the $guacamole in the double() function is not the same as the $bacon outside of it. Remember variables are placeholders. Think of it like the two are playing catch. In the following line:

$bacon = double($bacon);

We are passing a value to the double() function. The function then catches the value as its argument.

function double($guacamole)

Now the variable has a new name, but the same value. In the example the value was 10, that part isn’t any different. Within the function the variable $guacamole now has a value of 10. Then it performs its multiplication and sends back the doubled value. The is the pass back.

return $guacmole *2;

And that brings us back to:

$bacon = double($bacon);

where the variable $bacon catches the return pass, and the variable $bacon is now equal to 20, which we see when we echo $bacon.

So it is like playing catch. If you are out in the yard playing catch with someone who doesn’t speak the same language as you and you toss the ball to your partner, when he catches it, he might call it a beisbol. When he throws it back to you, it is a baseball again, you both just had different words (variables) for it.

This is the official PHP manual that might help explain things if my metaphors made it clear as mud.

http://php.net/manual/en/language.variables.scope.php

In Summary

We covered functions, function arguments, string variables, numeric variables and variable scope. There is quite a bit more, but I already feel like this is a lot actually and hopefully you are well on your way to programming hell… I mean bliss. Please point out any errors or places where I could be more clear, but keep Thematic and WordPress specific questions for their respective forums. Best of luck.

View all posts in this series
This entry was posted in Understanding the Basics and tagged , , , . Bookmark the permalink. Trackbacks are closed, but you can post a comment.

4 Comments

  1. Posted October 15, 2012 at 12:06 am | Permalink

    Kathy- As a relative beginner, I am afraid that I am a few steps beneath your training in my learning curve, at present. But I want you to know that I am bookmarking your site … for future reference. Keep it up!
    THX, Ted

    • Posted October 15, 2012 at 1:15 am | Permalink

      You’re welcome Ted. Everyone starts out as a beginner! And as they say in the martial arts, “the moves of the master are the moves of the beginner”.

  2. nathan
    Posted November 1, 2012 at 2:18 am | Permalink

    Kathy, Just sent you a very long email – sorry? After looking thru your latest posts topics i see you have answered much of what i asked – I’m reading tonight and developing tomorrow! Thank you for giving me a productive break from decision making ! You Rock!

    • Posted November 1, 2012 at 2:39 am | Permalink

      wow nathan, that is a monster email. i’m gonna need a while to process that! also, as someone who needs direction myself, i am probably the last person to be giving you any direction! :) but i shall try.

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

This site's anti-spam requires Javascript; enable it and refresh this page to continue.

NoScript Users: Whitelist the domain services.wpspamhammer.com and refresh.