8. Coding Horrors – The Ring
$studentArray = array();
$query1 = "SELECT * FROM students ORDER BY name";
$studentList = mysqli_query($conn,$query1)
or die ("cannot query the table1: " . mysqli_error($conn));
while ($student = mysqli_fetch_array($studentList)) {
$studentId = $student['id'];
$courses = array();
$query2 = "SELECT * FROM classes WHERE studentid='$studentId'";
$classList = mysqli_query($conn,$query2)
or die ("cannot query the table2: " . mysqli_error($conn));
while ($class = mysqli_fetch_array($classList)) {
array_push($classes, $class['name']);
}
array_push($studentArray, array($studentId, $student['name'], implode(', ', $classes)));
}
9. Coding Horrors – The Ring
SELECT s.id,
s.name,
group_concat(c.name separator ', ')
FROM students s
LEFT JOIN classes c
ON c.studentid = s.id
GROUP BY s.id,
s.name
ORDER BY s.name;
23. Coding Horrors – The Mummy
Returns
$q = "SELECT id, name FROM test WHERE name like '%:foo%'";
$s = "carrot";
$sth = $dbh->prepare($q);
$sth->bindParam(':foo', $s);
$sth->execute();
24. Coding Horrors – The Mummy
Returns
$q = "SELECT id, name FROM test WHERE name like :foo";
$s = "carrot";
$s = "%{$s}%";
$sth = $dbh->prepare($q);
$sth->bindParam(':foo', $s);
$sth->execute();
25. Coding Horrors – The Mummy
Returns
$q = "SELECT * FROM posts WHERE post_title LIKE :q OR post_text LIKE :q";
$s = "carrot";
$s = "%{$s}%";
$sth = $dbh->prepare($q);
$sth->bindParam(':q', $s);
$sth->execute();
26. Coding Horrors – The Mummy
Returns
$q = "SELECT * FROM posts WHERE post_title LIKE :q1 OR post_text LIKE :q2";
$s = "carrot";
$s = "%{$s}%";
$sth = $dbh->prepare($q);
$sth->bindParam(':q1', $s);
$sth->bindParam(':q2', $s);
$sth->execute();
29. Coding Horrors – Halloween
function dates() {
$startDate = new DateTime();
$endDate = (clone $startDate)
->add(new DateInterval('P1Y'));
$dateRange = new DatePeriod($startDate, new DateInterval('P1M'), $endDate);
foreach($dateRange as $date) {
yield $date;
}
}
foreach(dates() as $date) {
echo $date->format("M Y") . PHP_EOL;
}
30. Coding Horrors – Halloween
Jan 2018
Feb 2018
Mar 2018
Apr 2018
May 2018
Jun 2018
Jul 2018
Aug 2018
Sep 2018
Oct 2018
Nov 2018
Dec 2018
Jan 2018
Mar 2018
Apr 2018
May 2018
Jun 2018
Jul 2018
Aug 2018
Sep 2018
Oct 2018
Nov 2018
Dec 2018
Jan 2019
39. Coding Horrors – Se7en
1 => January
2 => February
3 => March
4 => April
5 => May
6 => June
7 => July
0 => September
10 => October
11 => November
12 => December
45. Coding Horrors – Dawn of the Dead
/**
* Convert a date from PHP to Excel
*
* @param mixed $dateValue unix timestamp or datetime object
* @param string $timezone Optional timezone name for adjustment from UTC
* @return mixed Excel date/time value
**/
public function PHPToExcel(DateTimeImmutable $dateValue, DateTimeZone $timezone = null) {
// Do stuff
}
48. Who am I?
Mark Baker
Design and Development Manager
InnovEd (Innovative Solutions for Education) Ltd
Coordinator and Developer of:
Open Source PHPOffice library
PHPExcel (PHPSpreadsheet), PHPWord, PHPPresentation (formerly PHPPowerPoint), PHPProject, PHPVisio
Minor contributor to PHP core
@Mark_Baker
https://github.com/MarkBaker
http://uk.linkedin.com/pub/mark-baker/b/572/171
http://markbakeruk.net
Notas do Editor
Friday the 13th
Using varchar for dates in a database table
The Ring (original Japanese version, not the US remake)
Unnecessary (and expensive) loops
Single database query to achieve the same result
The Mummy (1999 version)
Misunderstood bindings
Night of the Living Dead
This is what your version control is for!
And if you have to leave commented code in your files, then provide an explanation of why it is commented.
Static Analysis tools should pick this up
Don’t rely on PHP’s optimisation to clean up the redundant code, it still impairs readability, and won’t clean up the unused but mandatory $options argument
Static Analysis tools should pick this up too
The Ring (US Remake)
There are times when you simply shouldn’t try to copy a great original
The Mummy Returns
More binding misunderstandings
Can’t duplicate the same named parameter
Can’t duplicate the same named parameter
Halloween
31st of October
Alien
This code actually works without issue;
BUT the PHP Docs are pretty explicit here....
Symptomatic of a developer that doesn't understand basic PHP Functions, comparisons and datatypes; and/or didn't read the PHP Documentation: when you find an issue like this nesting in the codebase, there are certain to be other problems.
Se7en
Seven is the highest digit in octal notation
Gives a "Parse error: Invalid numeric literal" in PHP7....
What do you mean you're not running php7 yet?
Gives a "Parse error: Invalid numeric literal" in PHP7....
What do you mean you're not running php7 yet?
Scream
Besides the inefficiency of suppressing the errors, this also hides problems
Dawn of the Dead
Docblocks are no longer in synch with the code
Building the docs should highlight this mismatch
Don’t let these code smells come back to haunt you again and again