From 4b564bdf86e7fd3022fc38d3180811ae3f96d451 Mon Sep 17 00:00:00 2001 From: Marcin Wawrzyniak Date: Tue, 7 Aug 2012 12:31:28 +0200 Subject: [PATCH 1/6] Created "The Basics" section Including common '=' operator misunderstandings, unneded if/else, ternary and short_tags --- _posts/05-03-01-The-Basics.md | 119 ++++++++++++++++++++++++++++++++++ 1 file changed, 119 insertions(+) create mode 100644 _posts/05-03-01-The-Basics.md diff --git a/_posts/05-03-01-The-Basics.md b/_posts/05-03-01-The-Basics.md new file mode 100644 index 0000000..149219f --- /dev/null +++ b/_posts/05-03-01-The-Basics.md @@ -0,0 +1,119 @@ +--- +isChild: true +--- + +## The Basics + +In this paragraph you will get familiar with a few of the most common beginner mistakes. + + +### Using correct number of = + +The most common mistake is not to distinguish three different ``=`` operators: + + +1. ``=`` which means assignment, +2. ``==`` which means comparison **without** type checking (equality), +3. ``===`` which means comparison **with** type checking (identicality) + +Comparison operators are used always when you want to make sure that two variables or values are equal, for example: + +{% highlight php %} +if ($user->isAuthorized() === true) { +{% endhighlight %} +But you can also use assignment operators within condition blocks: + +{% highlight php %} +if ( ($myObject = Cache::read('my-cache-key')) === false ) { +{% endhighlight %} +Notice: first, the assignment is made, which is exactly what we want, and then it is evaluated against ``boolean false``. + +Not understanding comparison operators may result in logic errors. One of the most common happens when using ``strpos``. + +{% highlight php %} +$str = strpos('http://phptherightway.com', 'http://'); +{% endhighlight %} + +Notice: ``$str`` will become ``integer`` 0 as ``'http://'`` is found right at the beginning. +Keep in mind that when no occurences is found, ``strpos`` will return ``boolean false``. + +So following code will cast ``integer`` 0, to ``boolean`` false: + +{% highlight php %} +if ($str) { +{% endhighlight %} + +Which is clearly a logic error, because the ``http://`` substring was found. + +That's where `comparison operator with type checking` comes in: + +{% highlight php %} +if ($str !== false) { +{% endhighlight %} + +No type casting will occur, and that's why your logic is fine. + +### Unneded if/else and ternary + +Beginners very often tend to write following code: + +{% highlight php %} +if($user->isAuthorized()) { + return true; +} else { + return false; +} +{% endhighlight %} + +Notice that isAuthorized() is actually a ``boolean``, so you can write: + +{% highlight php %} +return $user->isAuthorized(); +{% endhighlight %} + +Another very common example would be: + +{% highlight php %} +if ($user->isAuthorized()) { + echo 'User authorized'; +} else { + echo 'Authorization error.'; +} +{% endhighlight %} + +Here you can simply use ternary operator: + +{% highlight php %} +echo $user->isAuthorized() ? 'User authorized' : 'Authorization error.'; +{% endhighlight %} + +As you can see above, ternary operator is just a more compact form of an ``if`` block. + +Keep in mind, that when you need to use nested ``if`` blocks, it is not recommended to use ternary operator, +as it may result in unreadable and error prone code: + +{% highlight php %} +echo (true?'true':false?'t':'f'); +{% endhighlight %} + +### Short tags + +Since PHP 5.4 , short tags are always safe to use. Regardless : + +``short_open_tag = Off`` + +Short tags are especially convenient to use in your presentation layer and that's where you should incorporate them. + +_When using PHP < 5.4, be aware that if short tags are supported or not, depends on your php.ini settings._ + +So, enjoy writing: + +{% highlight php %} +getLogin() ?> +{% endhighlight %} + +Instead of: + +{% highlight php %} +getLogin() ?>`` +{% endhighlight %} \ No newline at end of file From f8fc0e147d6818bb793422df9ac8d659a13dbaf6 Mon Sep 17 00:00:00 2001 From: Marcin Wawrzyniak Date: Thu, 9 Aug 2012 21:45:18 +0200 Subject: [PATCH 2/6] * fixed a typo --- _posts/05-03-01-The-Basics.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_posts/05-03-01-The-Basics.md b/_posts/05-03-01-The-Basics.md index 149219f..498630c 100644 --- a/_posts/05-03-01-The-Basics.md +++ b/_posts/05-03-01-The-Basics.md @@ -53,7 +53,7 @@ if ($str !== false) { No type casting will occur, and that's why your logic is fine. -### Unneded if/else and ternary +### Unnecessary if/else and ternary Beginners very often tend to write following code: From 6dc756bed51ed2db12b081a6ba9f3dfb4251bbdf Mon Sep 17 00:00:00 2001 From: Marcin Wawrzyniak Date: Thu, 9 Aug 2012 22:17:32 +0200 Subject: [PATCH 3/6] Created print.css for printing --- _layouts/default.html | 179 +++++++++++++++++++++--------------------- styles/print.css | 12 +++ 2 files changed, 102 insertions(+), 89 deletions(-) create mode 100644 styles/print.css diff --git a/_layouts/default.html b/_layouts/default.html index 7bcd73c..a233022 100644 --- a/_layouts/default.html +++ b/_layouts/default.html @@ -1,89 +1,90 @@ - - - - - {% if page.title %}{{ page.title }} - {% endif %}PHP: The Right Way - - - - - - - - - - - - - - - - - -
- - - Fork me on GitHub - - - - {{ content }} - -
- - - - - - + + + + + {% if page.title %}{{ page.title }} - {% endif %}PHP: The Right Way + + + + + + + + + + + + + + + + + + +
+ + + Fork me on GitHub + + + + {{ content }} + +
+ + + + + + diff --git a/styles/print.css b/styles/print.css new file mode 100644 index 0000000..e9b865a --- /dev/null +++ b/styles/print.css @@ -0,0 +1,12 @@ +body, .site-title, h1, h2, h3{ + font-family: 'Georgia' !important; +} + + +nav.site-navigation, a.fork-me, a.top{ + display:none; +} + +div.site-content{ + padding: 20px 40px 40px 20px; +} \ No newline at end of file From 78be54a75ae7898a6284c732b0e799086ab73c28 Mon Sep 17 00:00:00 2001 From: Marcin Wawrzyniak Date: Thu, 9 Aug 2012 22:18:49 +0200 Subject: [PATCH 4/6] Fixed line endings... --- _layouts/default.html | 180 +++++++++++++++++++++--------------------- 1 file changed, 90 insertions(+), 90 deletions(-) diff --git a/_layouts/default.html b/_layouts/default.html index a233022..aeb97c5 100644 --- a/_layouts/default.html +++ b/_layouts/default.html @@ -1,90 +1,90 @@ - - - - - {% if page.title %}{{ page.title }} - {% endif %}PHP: The Right Way - - - - - - - - - - - - - - - - - - -
- - - Fork me on GitHub - - - - {{ content }} - -
- - - - - - + + + + + {% if page.title %}{{ page.title }} - {% endif %}PHP: The Right Way + + + + + + + + + + + + + + + + + + +
+ + + Fork me on GitHub + + + + {{ content }} + +
+ + + + + + From 6824c3ce3476d598061c37508c041587c4eee9e6 Mon Sep 17 00:00:00 2001 From: Marcin Wawrzyniak Date: Thu, 9 Aug 2012 22:22:01 +0200 Subject: [PATCH 5/6] Revert "* fixed a typo" This reverts commit f8fc0e147d6818bb793422df9ac8d659a13dbaf6. --- _posts/05-03-01-The-Basics.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_posts/05-03-01-The-Basics.md b/_posts/05-03-01-The-Basics.md index 498630c..149219f 100644 --- a/_posts/05-03-01-The-Basics.md +++ b/_posts/05-03-01-The-Basics.md @@ -53,7 +53,7 @@ if ($str !== false) { No type casting will occur, and that's why your logic is fine. -### Unnecessary if/else and ternary +### Unneded if/else and ternary Beginners very often tend to write following code: From a5076f05406b70f6c4159352a1d5a6019bbc9de8 Mon Sep 17 00:00:00 2001 From: Marcin Wawrzyniak Date: Thu, 9 Aug 2012 22:22:07 +0200 Subject: [PATCH 6/6] Revert "Created "The Basics" section" This reverts commit 4b564bdf86e7fd3022fc38d3180811ae3f96d451. --- _posts/05-03-01-The-Basics.md | 119 ---------------------------------- 1 file changed, 119 deletions(-) delete mode 100644 _posts/05-03-01-The-Basics.md diff --git a/_posts/05-03-01-The-Basics.md b/_posts/05-03-01-The-Basics.md deleted file mode 100644 index 149219f..0000000 --- a/_posts/05-03-01-The-Basics.md +++ /dev/null @@ -1,119 +0,0 @@ ---- -isChild: true ---- - -## The Basics - -In this paragraph you will get familiar with a few of the most common beginner mistakes. - - -### Using correct number of = - -The most common mistake is not to distinguish three different ``=`` operators: - - -1. ``=`` which means assignment, -2. ``==`` which means comparison **without** type checking (equality), -3. ``===`` which means comparison **with** type checking (identicality) - -Comparison operators are used always when you want to make sure that two variables or values are equal, for example: - -{% highlight php %} -if ($user->isAuthorized() === true) { -{% endhighlight %} -But you can also use assignment operators within condition blocks: - -{% highlight php %} -if ( ($myObject = Cache::read('my-cache-key')) === false ) { -{% endhighlight %} -Notice: first, the assignment is made, which is exactly what we want, and then it is evaluated against ``boolean false``. - -Not understanding comparison operators may result in logic errors. One of the most common happens when using ``strpos``. - -{% highlight php %} -$str = strpos('http://phptherightway.com', 'http://'); -{% endhighlight %} - -Notice: ``$str`` will become ``integer`` 0 as ``'http://'`` is found right at the beginning. -Keep in mind that when no occurences is found, ``strpos`` will return ``boolean false``. - -So following code will cast ``integer`` 0, to ``boolean`` false: - -{% highlight php %} -if ($str) { -{% endhighlight %} - -Which is clearly a logic error, because the ``http://`` substring was found. - -That's where `comparison operator with type checking` comes in: - -{% highlight php %} -if ($str !== false) { -{% endhighlight %} - -No type casting will occur, and that's why your logic is fine. - -### Unneded if/else and ternary - -Beginners very often tend to write following code: - -{% highlight php %} -if($user->isAuthorized()) { - return true; -} else { - return false; -} -{% endhighlight %} - -Notice that isAuthorized() is actually a ``boolean``, so you can write: - -{% highlight php %} -return $user->isAuthorized(); -{% endhighlight %} - -Another very common example would be: - -{% highlight php %} -if ($user->isAuthorized()) { - echo 'User authorized'; -} else { - echo 'Authorization error.'; -} -{% endhighlight %} - -Here you can simply use ternary operator: - -{% highlight php %} -echo $user->isAuthorized() ? 'User authorized' : 'Authorization error.'; -{% endhighlight %} - -As you can see above, ternary operator is just a more compact form of an ``if`` block. - -Keep in mind, that when you need to use nested ``if`` blocks, it is not recommended to use ternary operator, -as it may result in unreadable and error prone code: - -{% highlight php %} -echo (true?'true':false?'t':'f'); -{% endhighlight %} - -### Short tags - -Since PHP 5.4 , short tags are always safe to use. Regardless : - -``short_open_tag = Off`` - -Short tags are especially convenient to use in your presentation layer and that's where you should incorporate them. - -_When using PHP < 5.4, be aware that if short tags are supported or not, depends on your php.ini settings._ - -So, enjoy writing: - -{% highlight php %} -getLogin() ?> -{% endhighlight %} - -Instead of: - -{% highlight php %} -getLogin() ?>`` -{% endhighlight %} \ No newline at end of file