mirror of
https://github.com/codeguy/php-the-right-way.git
synced 2025-08-08 15:06:30 +02:00
Update The-Basics.md
Tidying up the wordyness, removing overused and thus redundant variable assignments and improving explanations in replacement for quick & short comments.
This commit is contained in:
@@ -285,11 +285,12 @@ stacked/nested, it is advised to use one per line for readability.
|
|||||||
<?php
|
<?php
|
||||||
$a = 5;
|
$a = 5;
|
||||||
echo ($a == 5) ? 'yay' : 'nay';
|
echo ($a == 5) ? 'yay' : 'nay';
|
||||||
|
{% endhighlight %}
|
||||||
|
|
||||||
vs.
|
In comparison, here is an example that sacrifices all forms of readability for the sake of reducing the line count.
|
||||||
|
|
||||||
// nested ternary
|
{% highlight php %}
|
||||||
$b = 10;
|
<?php
|
||||||
echo ($a) ? ($a == 5) ? 'yay' : 'nay' : ($b == 10) ? 'excessive' : ':('; // excess nesting, sacrificing readability
|
echo ($a) ? ($a == 5) ? 'yay' : 'nay' : ($b == 10) ? 'excessive' : ':('; // excess nesting, sacrificing readability
|
||||||
{% endhighlight %}
|
{% endhighlight %}
|
||||||
|
|
||||||
@@ -339,42 +340,20 @@ vs
|
|||||||
<?php
|
<?php
|
||||||
$a = 3;
|
$a = 3;
|
||||||
return $a == 3 ? "yay" : "nope"; // return yay or nope if $a == 3
|
return $a == 3 ? "yay" : "nope"; // return yay or nope if $a == 3
|
||||||
|
|
||||||
{% endhighlight %}
|
{% endhighlight %}
|
||||||
|
|
||||||
It is down to the judgement of the developer where and when to use this method to improve code readability as it is debatable what is an improvement and what is a hinderance. Here is an example where it might be wise to use bracketing.
|
Bracketing also affords us the capability of creating unions within a statement block where the block will be checked as a whole. Such as this example below which will return true if both ($a == 3 and $b == 4) are true and $c == 5 is also true.
|
||||||
|
|
||||||
{% highlight php %}
|
{% highlight php %}
|
||||||
<?php
|
<?php
|
||||||
$a = 3;
|
return ($a == 3 && $b == 4) && $c == 5;
|
||||||
$b = 4;
|
|
||||||
$c = 5;
|
|
||||||
|
|
||||||
return $a == 3 || $b === 4 || $c !== 5; // return true if $a == 3 or $b === 4(value and type) or $c !== 5(value and type)
|
|
||||||
|
|
||||||
vs
|
|
||||||
|
|
||||||
<?php
|
|
||||||
$a = 3;
|
|
||||||
$b = 4;
|
|
||||||
$c = 5;
|
|
||||||
|
|
||||||
return ($a == 3) && ($b === 4) && ($c !== 5); // return true if $a == 3 or $b === 4(value and type) or $c !== 5(value and type)
|
|
||||||
|
|
||||||
{% endhighlight %}
|
{% endhighlight %}
|
||||||
|
|
||||||
Another reason to utilise bracketing is the ability to create unions within a statement block where the block will be checked as a whole. Such as:
|
Another example is the snippet below which will return true if ($a != 3 AND $b != 4) OR $c == 5.
|
||||||
|
|
||||||
{% highlight php %}
|
{% highlight php %}
|
||||||
<?php
|
<?php
|
||||||
$a = 3;
|
return ($a != 3 && $b != 4) || $c == 5;
|
||||||
$b = 4;
|
|
||||||
$c = 5;
|
|
||||||
|
|
||||||
return ($a == 3 && $b == 4) && $c == 5; // return true if both ($a == 3 and $b == 4) are true and $c == 5 is true.
|
|
||||||
return ($a != 3 && $b != 4) || $c == 5; // return true if ($a != 3 AND $b != 4) OR $c == 5
|
|
||||||
return (($a == $b || $a == $c) && $b != 4) || $c == 5; // return true if (($a == $b or $a == $c) and $b != 4) OR $c == 5
|
|
||||||
|
|
||||||
{% endhighlight %}
|
{% endhighlight %}
|
||||||
|
|
||||||
* [Ternary operators](http://php.net/manual/en/language.operators.comparison.php)
|
* [Ternary operators](http://php.net/manual/en/language.operators.comparison.php)
|
||||||
|
Reference in New Issue
Block a user