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:

$char = "z";
$char++; // is now aa

 


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.

"0.123" == ".123"; // is true

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

"0.123" === ".123"; // is false

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:

a, b, _, d = "1 2 3 4".split()

and in PHP:

list($a, $b, , $d) = array("1","2","3","4");

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.

$line = array(
    'name' => 'Peter',
    'age' => 27,
    'location' => 'USA');

function do_something($line) {
    extract($line);
    echo $name, $age, $location;
}

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

$line = array(
    'name' => 'Peter',
    'age' => 27,
    'location' => 'USA');

$print_pair = function ($value, $key) {
    print("Value: $value, Key: $key\n");
};

array_walk($line, $print_pair);

// Value: Peter, Key: name
// Value: 27, Key: age
// Value: USA, Key: location

array_reduce() is actually reduce, neat.

$multiply = function ($total, $value) {
    $total *= $value;
    return $total;
};

echo array_reduce(range(1,4), $multiply, 1); //24

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.

$line = array(
    'name' => 'Peter',
    'age' => 27,
    'location' => 'USA');

var_dump($line);

$flipped_line = array_flip($line);

var_dump($flipped_line);

/* 
array(3) {
["name"]=>
  string(5) "Peter"
["age"]=>
  int(27)
["location"]=>
  string(3) "USA"
}

array(3) {
["Peter"]=>
  string(4) "name"
[27]=>
  string(3) "age"
["USA"]=>
  string(8) "location"
}

*/

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.

class Person {
  public $name = "Adam";
    function print_name() {
      echo "My name is {$this->name}\n";
    }
}

$a_person = new Person;
$a_person->print_name();

echo "His name is really {$a_person->name}\n";

// My name is Adam
// His name is really Adam

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.