A while ago, before I decided to learn programming full-time, I tried my hand at PHP. I completed about 60% of the PHP track on Codecademy before deciding to refocus my efforts on Ruby. Now that I have a firm grounding in Ruby, I'd like to revisit PHP and explore some basic differences between the two languages.

PHP is a great general purpose language for web development, and is used on a huge number of sites. Similarly to Ruby templating languages like ERB or Haml, PHP can be embedded directly in HTML code, and is interpreted on the server, which sends HTML to be rendered by the client. Here are some of the differences I've found:

Booleans

Ruby takes a liberal approach to what can be considered truthy — everything except `false` and `nil` is considered truthy.

foo = "hello"       # => true
foo = ""            # => true
foo = 5             # => true
foo = 0             # => true
foo = [1, 2]        # => true
foo = []            # => true

PHP takes a more conservative approach — anything that is empty is considered false.

$foo = "hello"      // true
$foo = ""           // false
$foo = 5            // true
$foo = 0            // false
$foo = array(1, 2)  // true
$foo = array()      // false

Arrays and hashes/associative arrays

There's a bit of difference in the terminology between Ruby and PHP here. Ruby has two separate data structures called arrays and hashes. Arrays are simple ordered lists, like `[1, 2, 3, 4]`, while hashes are a list of key-value pairs. A Ruby hash looks like this:

{ :bob => "123 Main St.", :jane => "48 Chestnut St.", :steve => "307 Flatbush Ave." }

In PHP, a hash (or associative array) is simply a type of array. Take the following example:

$foo = array(1, 2, 3, 4);
echo gettype($foo);         // => array

$foo = array();
$foo["bob"] = "123 Main St.";
$foo["jane"] = "48 Chestnut St.";
$foo["steve"] = "307 Flatbush Ave.";
print_r($foo);
// => Array ( [bob] => 123 Main St. [jane] => 48 Chestnut St. [steve] => 307 Flatbush Ave. )

These are just a few basic differences I noticed — as I continue to learn PHP, I'll be sure to post more!

Learning PHP

A great place to start learning PHP is the PHP Development track on Treehouse. Once you've got a hang of the basics, check out some of Treehouse's other PHP courses.

Pro tip: If you live, work, attend school, or own property anywhere in New York State, you can get a free Queens Library card, which allows you to access Treehouse for free!

Posted
AuthorIan Valentine

At Flatiron School, we spend a lot of time programming in Ruby. As I've progressed in my learning, one of the challenges I've faced is trying to understand how inheritance works in JavaScript. Unlike Ruby, which uses classes, JavaScript uses prototypes, and the syntax is entirely different. What exactly is a prototype, and how does inheritance work in JavaScript as compared to Ruby?

In Ruby, objects inherit from classes. For example, here's a simple object:

x = "foo"

If you check the class of this object, Ruby will tell you that it is a string.

x.class # => String

Another way of thinking about this is that `x` is an instance of the `String` class. Therefore, you could also create the same object this way:

x = String.new("foo")

In JavaScript, things work differently. Unlike Ruby, JavaScript does not have classes — objects simply inherit from other objects. Here's an example:

function some_function() { return "foo"};

Let's create a new instance of this function:

some_other_function = new some_function();

Now, we can call `constructor` on `some_other_function`…

some_other_function.constructor // => function some_function()

…and we can see that `some_other_function` is an instance of `some_function`. The return value of the `constructor` method answers the question "Which object is this object a clone of?"

That's it! Though the idea of prototypes may seem confusing at first, the concept of inheritance remains the same — it's just a matter of what type of object an object can inherit from. I hope this helps clarify a small but important difference between Ruby and JavaScript. Happy programming!

Posted
AuthorIan Valentine

As I've progressed in my learning of web development, I've noticed that many fellow developers (inside and outside of Flatiron) use Sublime Text as their text editor of choice. In fact, Sublime Text is the editor recommended by Flatiron to incoming students. I've used Sublime Text, and it's great — it has tons of useful keyboard shortcuts, supports multiple selections, integrates with Git, and is extensible via the Sublime API.

While these are all great things, one thing about Sublime Text is decidedly not great: keep using it for a while, and it won't be long before you're greeted with this:

This dialog box appears every once in a while after saving. While I don't begrudge the developers of Sublime Text wanting to be paid for their hard work, as a student I'm not exactly flush with cash, so the $70 asking price to make the box go away is a bit steep.

Fortunately, there is a fine alternative that is totally free: Atom. Many of the features that exist in Sublime Text are also present in Atom — in fact, I've yet to come across a feature in Sublime that doesn't also exist in Atom.

One huge advantage that Atom has is that just over one year ago, GitHub decided to make Atom an open source project. In that time, Atom has made huge strides — and it hasn't even hit 1.0 yet. It's also endlessly customizable with packages and themes… check out this theme that makes you look like a 1337 h4x0r.

For some perspective: the last time I was taking a part-time programming course (in 2013), Atom didn't yet exist. (I used TextWrangler at the time.)

So far, my only complaint with Atom is that it takes longer to load, but I have no doubt that its performance will improve with time. If you want a powerful text editor but don't have $70 burning a hole in your pocket, give Atom a try — you will love it!

Posted
AuthorIan Valentine

One of the main reasons I love the web is that there are so many apps that work in conjunction with one another. In recent years, entire ecosystems of apps have sprouted up that work with Facebook and Twitter — some simply use them for authentication, while many others use your data from these services to customize your experience.

In the past, this meant that if you wanted to use your Facebook or Twitter account with a third-party app, you would need to provide your Facebook or Twitter username and password to these services. From a security standpoint, this is risky — with your password, any person (or any app) has full access to your account and can do whatever it wants, including changing your associated e-mail address and password, thereby locking you out of your account.

To solve this problem and make it easy to securely use third-party apps with services like Facebook and Twitter (and many others), a group of engineers created OAuthRather than give a third-party app your Twitter username and password (for example), OAuth allows the third-party app to redirect you to Twitter, where you can log in and grant permission for the third-party app to perform a limited set of actions and/or access a limited set of information. If you've ever connected a third-party app to your Twitter account, you've used OAuth — likely without even knowing it.

Eran Hammer's guide to OAuth 1.0 describes it as the "valet key for the Web," which I find to be a perfect metaphor. Rather than give the valet your regular car key, which allows full access to all areas of the car, you can provide a valet key which permits entry into the car but allows the trunk and glove box to remain secure.

Another benefit of OAuth is that if a third-party app you've authorized does something you don't like, you can easily revoke that app's access to your account — or even just revoke specific permissions — without having to change your password.

For these reasons (and many others!), OAuth is an excellent way for developers to work with a long list of services. In my next post, I'll explore OAuth in more depth.

Posted
AuthorIan Valentine

In the three weeks I've completed so far of the Web Development Immersive course at the Flatiron School, I've learned a lot. I've worked with the command line, Git, Ruby, and SQL — just a few of the many languages, systems, and tools developers use to accomplish their work. Among all of these, one stands out as singularly useful and powerful: regular expressions.

Regular expressions are a sequence of characters that denote a pattern to be searched for within a block of text. When you need to find some text that matches some known criteria, but don't know what the actual words themselves are, regular expressions are a great tool to use.

For example, let's say you needed to find every instance of a person's first and last name within the following block of text:

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque varius risus quis nibh placerat, nec interdum quam varius. Nam ut laoreet turpis, sit amet pulvinar elit. Quisque posuere rutrum tortor, at maximus elit consequat quis. Duis non nisl in arcu commodo tempus. Sed vehicula varius dolor sed venenatis. Sed id urna eget massa Steve Jobs sodales efficitur. Suspendisse nisi lacus, varius id fringilla a, commodo ut libero. Nulla vestibulum aliquam eros eget vestibulum. Nullam et lobortis dui. Cras tempor lorem tempor massa tincidunt pharetra. Nunc viverra tempor eros non interdum. Aliquam erat volutpat. Vestibulum eu magna blandit, facilisis Bill Gates purus at, viverra erat. Donec viverra pulvinar arcu. Ut dolor augue, porttitor eu lacinia nec, aliquet sed leo. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus.

Vestibulum auctor molestie neque, maximus lacinia mauris euismod cursus. Duis quis turpis fringilla, scelerisque leo ac, tincidunt lacus. Vestibulum semper fermentum est, quis ultrices metus fringilla vel. Morbi laoreet pretium luctus. Pellentesque turpis sem, dignissim bibendum Jeff Bezos elit sit amet, lobortis vehicula nunc. Cras quis nisl a nulla mattis pellentesque sit amet nec urna. Suspendisse in magna non erat vestibulum feugiat. Quisque tristique dui sit amet dui vehicula, sit amet iaculis lacus tempor.

Not so easy, is it?

Now, imagine that the text you have to search through is long enough to fill an entire book. You're a busy person, so you don't have the time to read through an entire book just to find every instance of a person's name. Besides, even if you were to do that, you might miss more than a few names along the way. Luckily, regular expressions make it simple to find just what we're looking for — even when it's buried in mounds of text.

To find a person's first and last name, we can use the following regular expression:

[A-Z]\w+\s[A-Z]\w+

In English, this translates to "a capital letter followed by any number of word characters, a space, a capital letter, and any number of word characters."

Try pasting the text and regular expression above into Rubular, and what do we get?

Steve Jobs, Bill Gates, Jeff Bezos

Bingo!

This example is pretty simple, but regular expressions are useful in a wide variety of situations — one common application is to check that the text entered in the "e-mail address" field on a form is, in fact, a valid e-mail address. Another useful application is for validating dates.

The most useful sites I've come across for learning about and testing regular expressions are RegExr and Rubular — give them a try, and the next time you need to find a needle in a haystack, you can use your newfound skills to save the day!

Posted
AuthorIan Valentine