Default values in php functions

Hello all. 

Now then, I’m getting to grips with php again on the great Treehouse website.  This wouldn’t be first time I’ve tried to learn php. Oh no. I’ve tried to do it self taught, via online tutorial sites, through my university degree. So over come across it before and some of the things I’ve learned over the past week or so are not new to me at all.

But one thing that has caught my eye is the concept of default and optional variable arguments. I’ve seen in other languages that you can pass in variables to functions as placeholders for values input when you call a given function.


Here for example I’m passing in an array index as an parameter that matches a function argument, 

But what if you want to pass in something but give it a value before you call that value?  Well, you could do it like this.

function greet ($name = “friend”){
  echo “Hello $name”);
greet() outputs “Hello Friend”;
greet(“Mike”) outputs “Hello Mike”

A default argument is an argument passed through with the starting value.

Sometimes if an argument is not passed through, and you want it to have a starting value, no matter what, this is where a default argument will come to your assistance. If for whatever reason you didn’t specify something at the function call, php has a default value as a kind of backup, so it will always have something to display to the screen.

As well as default arguments, you can also have optional arguments.

You would declare these in the same way as a default argument.  Giving it the value of null.

e.g. setting optional argument = null makes an argument optional.

function greet ($name = “friend”,
                 $time_of_day = NULL) {
     echo “Hello. $name. Good $time_of_day

       } else }
 echo “Hello $name}

Default arguments prevent errors when passing through argument.

function (get_info($name, $title) {
  echo “$name has arrived. they are with us as a $title”;
get_info(“Mike”, “Frog”); Same order as default in arguments as function arguments read left to right.

function (get_info($name, $title = “friend”) {
  echo “$name has arrived. they are with us as a $title”;

Having the default argument is crucial as we can leave this out at function call and it would output Mike has arrived. they are with us as a friend, rather than leaving the second variable out completely.
It you were to have Null instead of friend as the second argument this makes the second argument option.

But we still need to to make that title exists as an argument or we would lose that output from the screen..

function (get_info($name, $title = Null) {
  if($title) {
  echo “$name has arrived. they are with us as a $title”;
} else {
  echo “$name has arrived, welcome!

These are just a few examples of how useful functions can be and what can be done with basic functions, but it’s still only the start.  There’s still so much for me to learn and less time as more work happily comes in for me business. But the plan remains to continue to learn more php to stand me in good stead to get into WordPress development. And I’m throughly enjoying my new education in php at the moment.


A short blog: Responsive Web Design

I’ve been in discussions recently with Auto Cleanse North East; about mobile web design work.  I’m delighted to say we’re both ready now for building to place for the responsive design and I’m in the process of doing this.
In the last month I’ve done a lot of promotion on the back of the Google search algorithm change and about how websites not optimised for mobile would negatively impact search ranking and you should get it looked at asap.
I still believe that, although when I was doing this, I didn’t mean to imply a doomsday scenario that would destroy your business on day 1 of no responsiveness on your website after the change.
On the contrary it’s been over 2 weeks now since the change, and the ranking on hasn’t yet been changed although the next update on this is due in soon and I’m monitoring the situation.
This could change any time soon. However it’s not just ranking factor I’m concerned about with mobile websites.  It’s user experience too.
Let’s say you have a website, an e-commerce site for instance, complete with shopping cart and desktop interface.  It works great on desktop and some tablets. But hold up, what happens on mobile?  The “user agent” (or web browser in English) still has to download all that information your desktop browser has to and include all that information ,displaying it in such a small space.
You can get around this with your mobile’s touch gestures and just swipe your way across to the part of the website you want, and if that’s fine for you then that’s great. But it needn’t be like that.
More than half the web browsing around the world these days is done on a mobile phone, and increasingly on a tablet device.   If you want to improve the user experience for your website visitors who are not using a desktop computer than it really is worth your while getting your website sorted for responsive website design.
To talk with with me about mobile web design work get in touch through my website or drop me an email at
Thanks as ever for reading. 🙂

Catching up: What JGDM has been up to recently

Hi Everyone.

It’s been a while since my last blog. But it’s only because I’ve been so busy.  🙂  I’ve spent a lot of time learning new skills and honing my current ones so I’ve not had a lot of time for adding to my development website or to this blog.

I do have a couple coming up that I hope anyone who happens to be reading my posts useful 🙂

Since I last blogged I’ve devoted a lot of time to learning how to customise, and build a website without coding. I have plans to expand this to theme development for the future which involves PHP code but this is for the future.

I designed a website layout for a UK charity for a client of mine to develop into a WordPress Theme. Most websites are actually made with WordPress these days so this is a skill I’m hoping to pick up soon.

My social media work is starting to kick off a little bit.  Initially on a trial basis at least, I’m taking on the social media posts for High Impact Development UK, a local company who deliver leadership, team building and emotional intelligence coaching.  It’s been a pleasure so far to do and a joy to have the opportunity to build on social management skills and learn a little bit about Emotional Intelligence at the same time.

I’ve also been recently been commissioned to develop mobile websites for a couple of clients so pretty soon I’m going to be very busy indeed.  I’ll soon be talking more about this and a new item to my “Labs” website More on these later.

Thanks for reading. 🙂

Sole Proprietor and Designer: Jonnie Grieve Digital Media