in Back to PHP

My Way to PHP: Day 1 of 75

Ok, first day! I will start with the book Programming PHP. I’ve started to read a previous edition of this book years ago – it was the 2006 release, PHP 5.2 just came out. Then I decided to get more into Python.

My goal for today is to get to page 50 which should be pretty easy. Page 50 just covers the while construct. I decided to start off learning the theory first. I like having an idea before writing code. Also I’ve written PHP and PHP-like code (e.g. in C or JS) enough so that it isn’t detremental for my effort.

PHP is a simple yet powerful language designed for creating HTML content

Interesting that they self-identify like that. I’ve watched a talk by DHH about RoR 4 or so. He basically said that the same RoR. It’s a framework to generate HTML. And he wanted it to be good to create document-like stuff on the web.

almost 78% of usage on all the surveyed websites [is PHP] (W3Techs – May 2012)

I just looked up current statistics and now it’s 82% PHP. Followed by ASP (around 13%) and Java (2%).

Idea: Seasteading old-school browser game

While showering I had an idea for my final project. I thought about making one of those old school browser games. No flash, no animations, just good old multiplayer strategy in real-time.


Nice thing. If you increment a character it wraps around automatically:

 


Here’s something dangerous. String comparisons aren’t always lexical. If you have at least one string which is entirely numerical PHP will try to do an numeric cast and compare those strings numerically.

Thankfully, there’s === which doesn’t implicitly do type casting.


Variable parameters are a bit strange. Instead of defining something like

function a($a...) or function($a, &$rest) or whatever, you define the function with any parameters.

You can then use func_num_args() to get the number of arguments and func_get_arg($num) to get the argument. Or get an array of the parameters using func_get_args().


Half-complete type hinting: classes, callable, arrays work but scalars not. It throws a run-time error in case the type doesn’t match which is nice.


var_dump() is preferable to print_r(): better handling of booleans, won’t infinite recure, more details


htmlentities() takes 3 arguments. Inputstring, quoting and charset. I noticed in my IT security time that a lot of people used htmlentities() without the quoting argument. This only converts double quotes into the html entity but not single ones.

If you want to encode both use htmlentities($input, ENT_QUOTES);


list() is quite cool. It’s similar to assigning multiple variables in Python. Example in Python:

and in PHP:


extract() is great. It can take an associative array (aka hashmap) and create variables. It’s second argument, allows variations, e.g. prefixing all new variable names.


array_walk() is superb. I smiled when I read about that function. It’s nearly a map, nearly. :P


array_reduce() is actually reduce, neat.

I just wanted to write a map using reduce but saw that there’s even array_map() – hurray! Also array_filter(). Looking at all the array functions is definitely on my list and also checking out functional programming in PHP.


Updated Goals (18th October)

  • Relearn the basic syntax, keywords, constructs
  • Learn the intricacies: how does the interpreter work, the OOP system, etc.
  • Get a overview over the standard library
  • Learn the right and idiomatic way to do PHP development today
  • Learn the most important pitfalls
  • Learn a bit more about MySQL
  • Learn more about the PHP OOP and Design Patterns
  • Learn Symfony2
  • Write at least one web app using Symfony2 and its core components (templating, testing, forms, validation, security, caching, i18n)
  • Learn all the array functions
  • Learn about functional programming in PHP

array_flip() is also quite handy. It exchanges key with value.


The whole chapter on Arrays was pretty interesting. The last few pages were dedicated to use arrays as sets and to implement a stack. It’s kinda funny that arrays are so central to PHP but I’ve never heard somebody talk about it.


It’s nice that accessing methods and fields of objects is exactly the same syntax.


While thinking about different approaches to implement stuff in OOP instead of in a DB layer I thought that I need to read more stuff on that. Therefore, I added Patterns of Enterprise Application Architecture which sounds super enterprisy but isn’t that much.

Updated Goals (18th October #2)

  • Relearn the basic syntax, keywords, constructs
  • Learn the intricacies: how does the interpreter work, the OOP system, etc.
  • Get a overview over the standard library
  • Learn the right and idiomatic way to do PHP development today
  • Learn the most important pitfalls
  • Learn a bit more about MySQL
  • Learn more about the PHP OOP and Design Patterns
  • Learn Symfony2
  • Write at least one web app using Symfony2 and its core components (templating, testing, forms, validation, security, caching, i18n)
  • Learn all the array functions
  • Learn about functional programming in PHP
  • Read Patterns of Enterprise Application Architecture

Traits are interesting. It’s basically like a class with static functions which however gives all it’s functions to the class using it. I wonder how much it is used.


Progress status

In Progress

  • Programming PHP by Kevin Tatroe, Peter MacIntyre, Rasmus Lerdorf [206 of 540 pages]

Conclusion

So the first day is over and I felt motivated again.  I had the same or similar feels like in my youth where I just programmed and created something. Without thinking about the next 100 years. It sounds strange but it felt incredible freeing.

Write a Comment

Comment