Learning PHP from a Python perspective

When I started at Pixo 7 months ago, I knew python, but I was new to PHP.  It was relatively easy to make the transition, but there are a few differences that tripped me up for a while that I wanted to write down in a single place to share.

Optional Parameters Are (Sometimes) Required

In php, all function parameters are required unless you give them a default value.

function foo($var1, $var2="something") {
  // do stuff
  return;
}

In this case, $var1 is required whereas $var2 is optional with a default value of empty string.  It would be perfectly acceptable to call it like this:

foo("bar");

What if we wanted to add another optional parameter?

function foo($var1, $var2="something", $var3="else") {
  // do stuff
  return;
}

PHP will not let you override $var3 without also providing a value for $var2 even though it has a default value. So, if you wanted to keep the default value for $var2, you would need to pass it in like so:

foo("bar", "something", "different");

One or two arguments are not a problem. Three or more and this gets cumbersome. That is why it is common practice to bundle together the optional arguments into an array and handling the defaults within the function rather than the function definition.

function foo($var1, $var2="something", $var3="else") {
  // do stuff
  return;
}

foo("bar", Array("var2"=>"something", "var3"=>"else");

** However, as I’m writing up this post, I discovered that there is a way to pass a variable number of arguments using func-get-args, func-get-arg, and func-num-args which I didn’t notice at the time I encountered this problem.

For Loops

In addition to C style syntax, php also provides a foreach statement which can be used in two forms:

// list of values or objects
foreach ( $array as $obj ) { ... }

// associative list
foreach ( $array as $key=>$value ) { ... }

This is the inverse of Python’s syntax which is:

for element in list:
  # ...

for (x, y) in list_of_tuples:
  # ...

While learning Python’s syntax first may play a part in my bias, I think I still favor it because it mirrors set theory.  However that is more personal preference than anything else.

Substrings

To extract a substring, you need this command:

substr( $string, $start, $length)

I kept putting the end position in there for the longest time.  Here, I really miss python’s array slicing:

string[start, end] 

Troubleshooting

I don’t know how many times the magic of var_dump() has helped me arrive at a solution in 30 seconds vs. 30+ min of searching for my answer in documentation or a blog post somewhere.  var_dump() is your friend.  It is especially enlightening when troubleshooting objects you are unfamiliar with, which was often for me as I was learning WordPress.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s