SlideShare a Scribd company logo
1 of 70
Coding
Horrors
A Horror Film Fan’s Guide to
PHP Coding Nightmares
Coding
Horrors
Coding Horrors
Coding Horrors – Friday the 13th
Coding Horrors – Friday the 13th
// 3 month appointments
$date_to = date('d/m/Y');
$date_from = date("d/m/Y", strtotime(" +3 months"));
$get = "select * from appointments where date between '$date_from' and '$date_to'";
$get_connect = mysqli_query($con2, $get);
$get_rows = mysqli_num_rows($get_connect);
Coding Horrors – Friday the 13th
mysql> select * from appointments;
+----+-----------+------------------------+
| id | date | appointment |
+----+-----------+------------------------+
| 1 | 2/1/2018 | PHPNW January meetup |
| 2 | 6/2/2018 | PHPNW February Meetup |
| 3 | 6/3/2018 | PHPNW March Meetup |
| 4 | 3/4/2018 | PHPNW April Meetup |
| 5 | 1/5/2018 | PHPNW May Meetup |
| 6 | 5/6/2018 | PHPNW June Meetup |
| 7 | 3/7/2018 | PHPNW July Meetup |
| 8 | 7/8/2018 | PHPNW August Meetup |
| 9 | 4/9/2018 | PHPNW September Meetup |
| 10 | 2/10/2018 | PHPNW October Meetup |
| 11 | 6/11/2018 | PHPNW November Meetup |
| 12 | 4/12/2018 | PHPNW December Meetup |
+----+-----------+------------------------+
12 rows in set (0.01 sec)
mysql> select * from appointments
-> where date between '2/1/2018' and '5/1/2018’;
+----+-----------+------------------------+
| id | date | appointment |
+----+-----------+------------------------+
| 1 | 2/1/2018 | PHPNW January meetup |
| 4 | 3/4/2018 | PHPNW April Meetup |
| 7 | 3/7/2018 | PHPNW July Meetup |
| 9 | 4/9/2018 | PHPNW September Meetup |
| 10 | 2/10/2018 | PHPNW October Meetup |
| 12 | 4/12/2018 | PHPNW December Meetup |
+----+-----------+------------------------+
6 rows in set (0.00 sec)
Coding Horrors
Coding Horrors – The Ring
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)));
}
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;
Coding Horrors – The Ring
SELECT s.id,
s.name,
c.name
FROM students s
LEFT JOIN classes c
ON c.studentid = s.id
GROUP BY s.id,
s.name
ORDER BY s.name;
Coding Horrors
Coding Horrors – The Mummy
Coding Horrors – The Mummy
$Data = $connection->prepare(
"SELECT * FROM :TableName ORDER BY :OrderByColumn :OrderDirection LIMIT 10 OFFSET :Offset"
);
$Data->bindValue(':TableName', $TableName, PDO::PARAM_STR);
$Data->bindValue(':Offset', $Offset, PDO::PARAM_INT);
$Data->bindValue(':OrderByColumn', $OrderColumn, PDO::PARAM_STR);
$Data->bindValue(':OrderDirection', $OrderDirection, PDO::PARAM_STR);
$Data->execute();
Coding Horrors
Coding Horrors – Night of the
Living Dead
Coding Horrors – Night of the
Living Dead
function getAddressDetails() {
$data->name = 'Mark';
// $data->address = 'mi casa';
// $data->city = 'Manchester';
// $data->country = 'UK';
// return json_encode($data, JSON_UNESCAPED_UNICODE);
return json_encode($data);
}
Coding Horrors – Night of the
Living Dead
function getAddressDetails() {
$data->name = 'Mark’;
return json_encode($data);
}
git commit -m "Remove address, which is now accessed through a separate API call"
Coding Horrors – Night of the
Living Dead
function addValues($a, $b, $options) {
$sum = 0;
return $a + $b;
}
Coding Horrors – Night of the
Living Dead
function squareIt($bar) {
return $bar ** 2;
$bar *= $bar;
return $bar;
}
Coding Horrors
Coding Horrors – The Ring (US
Remake)
Coding Horrors – The Ring (US
Remake)
DRY
Don’t Repeat Yourself
Coding Horrors
Coding Horrors – Chain Letter
Coding Horrors – Chain Letter
mysql> select * from users;
+----+----------+----------------------------------+-----------+
| id | username | password | followers |
+----+----------+----------------------------------+-----------+
| 1 | Shirley | 5f4dcc3b5aa765d61d8327deb882cf99 | 8,6,10 |
| 2 | Deborah | d1133275ee2118be63a577af759fc052 | 4 |
| 3 | Julie | 25d55ad283aa400af464c76d713c07ad | 4,5 |
| 4 | Jane | 4297f44b13955235245b2497399d7a93 | 3,5 |
| 5 | Jennifer | d8578edf8458ce06fbc5bb76a58c5ca4 | 3,4 |
| 6 | Alison | 827ccb0eea8a706c4c34a16891f84e7b | 9,1 |
| 7 | Philipa | 8621ffdbc5698829397d97767ac13db3 | NULL |
| 8 | Sue | acc6f2779b808637d04c71e3d8360eeb | 1 |
| 9 | Annabel | 0d107d09f5bbe40cade3de5c71e9e9b7 | 6 |
| 10 | Cathy | 5badcaf789d3d1d09794d8f021f40f0e | 3,1 |
+----+----------+----------------------------------+-----------+
10 rows in set (0.00 sec)
Coding Horrors – Chain Letter
mysql> select * from users where followers like '%1%';
+----+----------+----------------------------------+-----------+
| id | username | password | followers |
+----+----------+----------------------------------+-----------+
| 1 | Shirley | 5f4dcc3b5aa765d61d8327deb882cf99 | 8,6,10 |
| 6 | Alison | 827ccb0eea8a706c4c34a16891f84e7b | 9,1 |
| 8 | Sue | acc6f2779b808637d04c71e3d8360eeb | 1 |
| 10 | Cathy | 5badcaf789d3d1d09794d8f021f40f0e | 3,1 |
+----+----------+----------------------------------+-----------+
4 rows in set (0.00 sec)
Coding Horrors – Chain Letter
mysql> select * from users where followers like '%,1,%';
Empty set (0.00 sec)
mysql> select * from users
-> where followers = '1' or followers like '1,%' or followers like '%,1,%' or followers like '%,1';
+----+----------+----------------------------------+---------+
| id | username | password | friends |
+----+----------+----------------------------------+---------+
| 6 | Alison | 827ccb0eea8a706c4c34a16891f84e7b | 9,1 |
| 8 | Sue | acc6f2779b808637d04c71e3d8360eeb | 1 |
| 10 | Cathy | 5badcaf789d3d1d09794d8f021f40f0e | 3,1 |
+----+----------+----------------------------------+---------+
3 rows in set (0.00 sec)
Coding Horrors – Chain Letter
mysql> select * from users where find_in_set(1,friends);
+----+----------+----------------------------------+---------+
| id | username | password | friends |
+----+----------+----------------------------------+---------+
| 6 | Alison | 827ccb0eea8a706c4c34a16891f84e7b | 9,1 |
| 8 | Sue | acc6f2779b808637d04c71e3d8360eeb | 1 |
| 10 | Cathy | 5badcaf789d3d1d09794d8f021f40f0e | 3,1 |
+----+----------+----------------------------------+---------+
3 rows in set (0.00 sec)
Coding Horrors
Coding Horrors – The Mummy
Returns
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();
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();
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();
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();
Coding Horrors
Coding Horrors – Halloween
Coding Horrors – Halloween
function monthList() {
$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(monthList() as $date) {
echo $date->format("M Y") . PHP_EOL;
}
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
Coding Horrors – Halloween
function monthList() {
$startDate = new DateTime('first day of this month');
$endDate = (clone $startDate)
->add(new DateInterval('P1Y'));
$dateRange = new DatePeriod($startDate, new DateInterval('P1M'), $endDate);
foreach($dateRange as $date) {
yield $date;
}
}
foreach(monthList() as $date) {
echo $date->format("M Y") . PHP_EOL;
}
Coding Horrors
Coding Horrors – Alien
Coding Horrors – Alien
if (strpos(" " . $fileURL, "s3://") >= 1) {
// Do something
}
Coding Horrors – Alien
Coding Horrors – Alien
if (strpos($fileURL, "s3://") !== false) {
// Do something
}
Coding Horrors
Coding Horrors – Se7en
Coding Horrors – Se7en
$calendar = [
01 => 'January',
02 => 'February',
03 => 'March',
04 => 'April',
05 => 'May',
06 => 'June',
07 => 'July',
08 => 'August',
09 => 'September',
10 => 'October',
11 => 'November',
12 => 'December',
];
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
Coding Horrors – Se7en
$calendar = [
1 => 'January',
2 => 'February',
3 => 'March',
4 => 'April',
5 => 'May',
6 => 'June',
7 => 'July',
8 => 'August',
9 => 'September',
10 => 'October',
11 => 'November',
12 => 'December',
];
Coding Horrors
Coding Horrors – Pan’s Labyrinth
Coding Horrors – Pan’s Labyrinth
public function __construct(
MagentoFrameworkModelContext $context,
MagentoFrameworkViewDesignInterface $design,
MagentoFrameworkRegistry $registry,
MagentoStoreModelAppEmulation $appEmulation,
MagentoStoreModelStoreManagerInterface $storeManager,
MagentoFrameworkAppRequestInterface $request,
MagentoNewsletterModelTemplateFilter $filter,
MagentoFrameworkAppConfigScopeConfigInterface $scopeConfig,
MagentoNewsletterModelTemplateFactory $templateFactory,
MagentoFrameworkFilterFilterManager $filterManager,
array $data = []
) {
parent::__construct($context, $design, $registry, $appEmulation, $storeManager, $data);
$this->_storeManager = $storeManager;
$this->_request = $request;
$this->_filter = $filter;
$this->_scopeConfig = $scopeConfig;
$this->_templateFactory = $templateFactory;
$this->_filterManager = $filterManager;
}
Coding Horrors – Pan’s Labyrinth
The ideal number of arguments for a function is zero (niladic). Next
comes one (monadic) followed closely by two (dyadic). Three
arguments (triadic) should be avoided where possible. More than three
(polyadic) requires very special justification—and then shouldn't be
used anyway.
Bob Martin – “Clean Code”
Coding Horrors
Coding Horrors – Scream
Coding Horrors – Scream
$sql = "SELECT MAX(id) AS max_page FROM videos";
$result = @$conn->query($sql);
$row = @mysql_fetch_array($result);
echo $row["max_page"];
Coding Horrors – Scream
@dns_get_record();
dns_get_record();
Coding Horrors – Scream
Warning: dns_get_record() expects at least 1 parameter, 0 given in
%filename% on %line%
Coding Horrors – Scream
set_error_handler(function($errno, $errstr, $errfile, $errline, array $errcontext) {
if (error_reporting() === 0) {
// error was suppressed with the @-operator
throw new ErrorException(
'WHY ARE YOU SUPPRESSING ERRORS RATHER THAN HANDLING THEM!'
);
// return false;
}
throw new ErrorException($errstr, 0, $errno, $errfile, $errline);
});
Coding Horrors – Scream
try {
@dns_get_record();
} catch (ErrorException $e) {
echo 'EXCEPTION: ', $e->getMessage(), PHP_EOL;
}
try {
dns_get_record();
} catch (ErrorException $e) {
echo 'EXCEPTION: ', $e->getMessage(), PHP_EOL;
}
Coding Horrors – Scream
EXCEPTION: WHY ARE YOU SUPPRESSING ERRORS RATHER THAN HANDLING THEM?
EXCEPTION: dns_get_record() expects at least 1 parameter, 0 given
Coding Horrors – Scream
Coding Horrors – Scream
• Scream
• PECL Extensions
• Disables the @ error control operator so that all errors are reported.
ini_set('scream.enabled', true);
Coding Horrors
Coding Horrors – Dawn of the Dead
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
}
Coding
Horrors
Coding
Horrors
Who am I?
Mark Baker
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

More Related Content

What's hot

Super Advanced Python –act1
Super Advanced Python –act1Super Advanced Python –act1
Super Advanced Python –act1Ke Wei Louis
 
Swift - 혼자 공부하면 분명히 안할테니까 같이 공부하기
Swift - 혼자 공부하면 분명히 안할테니까 같이 공부하기Swift - 혼자 공부하면 분명히 안할테니까 같이 공부하기
Swift - 혼자 공부하면 분명히 안할테니까 같이 공부하기Suyeol Jeon
 
{tidytext}と{RMeCab}によるモダンな日本語テキスト分析
{tidytext}と{RMeCab}によるモダンな日本語テキスト分析{tidytext}と{RMeCab}によるモダンな日本語テキスト分析
{tidytext}と{RMeCab}によるモダンな日本語テキスト分析Takashi Kitano
 
Teaching Your Machine To Find Fraudsters
Teaching Your Machine To Find FraudstersTeaching Your Machine To Find Fraudsters
Teaching Your Machine To Find FraudstersIan Barber
 
Groovy puzzlers jug-moscow-part 2
Groovy puzzlers jug-moscow-part 2Groovy puzzlers jug-moscow-part 2
Groovy puzzlers jug-moscow-part 2Evgeny Borisov
 
PHP and MySQL Tips and tricks, DC 2007
PHP and MySQL Tips and tricks, DC 2007PHP and MySQL Tips and tricks, DC 2007
PHP and MySQL Tips and tricks, DC 2007Damien Seguy
 
Debugging: Rules And Tools - PHPTek 11 Version
Debugging: Rules And Tools - PHPTek 11 VersionDebugging: Rules And Tools - PHPTek 11 Version
Debugging: Rules And Tools - PHPTek 11 VersionIan Barber
 
Crazy things done on PHP
Crazy things done on PHPCrazy things done on PHP
Crazy things done on PHPTaras Kalapun
 
令和から本気出す
令和から本気出す令和から本気出す
令和から本気出すTakashi Kitano
 
Doctrine fixtures
Doctrine fixturesDoctrine fixtures
Doctrine fixturesBill Chang
 
MongoDB全機能解説2
MongoDB全機能解説2MongoDB全機能解説2
MongoDB全機能解説2Takahiro Inoue
 
RxSwift 시작하기
RxSwift 시작하기RxSwift 시작하기
RxSwift 시작하기Suyeol Jeon
 
How to stand on the shoulders of giants
How to stand on the shoulders of giantsHow to stand on the shoulders of giants
How to stand on the shoulders of giantsIan Barber
 
MongoDBで作るソーシャルデータ新解析基盤
MongoDBで作るソーシャルデータ新解析基盤MongoDBで作るソーシャルデータ新解析基盤
MongoDBで作るソーシャルデータ新解析基盤Takahiro Inoue
 
Adventures in Optimization
Adventures in OptimizationAdventures in Optimization
Adventures in OptimizationDavid Golden
 
{tidygraph}と{ggraph}による モダンなネットワーク分析(未公開ver)
{tidygraph}と{ggraph}による モダンなネットワーク分析(未公開ver){tidygraph}と{ggraph}による モダンなネットワーク分析(未公開ver)
{tidygraph}と{ggraph}による モダンなネットワーク分析(未公開ver)Takashi Kitano
 

What's hot (20)

Pop3ck sh
Pop3ck shPop3ck sh
Pop3ck sh
 
Super Advanced Python –act1
Super Advanced Python –act1Super Advanced Python –act1
Super Advanced Python –act1
 
Swift - 혼자 공부하면 분명히 안할테니까 같이 공부하기
Swift - 혼자 공부하면 분명히 안할테니까 같이 공부하기Swift - 혼자 공부하면 분명히 안할테니까 같이 공부하기
Swift - 혼자 공부하면 분명히 안할테니까 같이 공부하기
 
{tidytext}と{RMeCab}によるモダンな日本語テキスト分析
{tidytext}と{RMeCab}によるモダンな日本語テキスト分析{tidytext}と{RMeCab}によるモダンな日本語テキスト分析
{tidytext}と{RMeCab}によるモダンな日本語テキスト分析
 
PHP 5.4
PHP 5.4PHP 5.4
PHP 5.4
 
Daily notes
Daily notesDaily notes
Daily notes
 
Teaching Your Machine To Find Fraudsters
Teaching Your Machine To Find FraudstersTeaching Your Machine To Find Fraudsters
Teaching Your Machine To Find Fraudsters
 
Groovy puzzlers jug-moscow-part 2
Groovy puzzlers jug-moscow-part 2Groovy puzzlers jug-moscow-part 2
Groovy puzzlers jug-moscow-part 2
 
PHP and MySQL Tips and tricks, DC 2007
PHP and MySQL Tips and tricks, DC 2007PHP and MySQL Tips and tricks, DC 2007
PHP and MySQL Tips and tricks, DC 2007
 
Debugging: Rules And Tools - PHPTek 11 Version
Debugging: Rules And Tools - PHPTek 11 VersionDebugging: Rules And Tools - PHPTek 11 Version
Debugging: Rules And Tools - PHPTek 11 Version
 
Crazy things done on PHP
Crazy things done on PHPCrazy things done on PHP
Crazy things done on PHP
 
令和から本気出す
令和から本気出す令和から本気出す
令和から本気出す
 
Doctrine fixtures
Doctrine fixturesDoctrine fixtures
Doctrine fixtures
 
MongoDB全機能解説2
MongoDB全機能解説2MongoDB全機能解説2
MongoDB全機能解説2
 
RxSwift 시작하기
RxSwift 시작하기RxSwift 시작하기
RxSwift 시작하기
 
How to stand on the shoulders of giants
How to stand on the shoulders of giantsHow to stand on the shoulders of giants
How to stand on the shoulders of giants
 
MongoDBで作るソーシャルデータ新解析基盤
MongoDBで作るソーシャルデータ新解析基盤MongoDBで作るソーシャルデータ新解析基盤
MongoDBで作るソーシャルデータ新解析基盤
 
You Got Async in my PHP!
You Got Async in my PHP!You Got Async in my PHP!
You Got Async in my PHP!
 
Adventures in Optimization
Adventures in OptimizationAdventures in Optimization
Adventures in Optimization
 
{tidygraph}と{ggraph}による モダンなネットワーク分析(未公開ver)
{tidygraph}と{ggraph}による モダンなネットワーク分析(未公開ver){tidygraph}と{ggraph}による モダンなネットワーク分析(未公開ver)
{tidygraph}と{ggraph}による モダンなネットワーク分析(未公開ver)
 

Similar to Coding Horrors

Simple Ways To Be A Better Programmer (OSCON 2007)
Simple Ways To Be A Better Programmer (OSCON 2007)Simple Ways To Be A Better Programmer (OSCON 2007)
Simple Ways To Be A Better Programmer (OSCON 2007)Michael Schwern
 
Short Intro to PHP and MySQL
Short Intro to PHP and MySQLShort Intro to PHP and MySQL
Short Intro to PHP and MySQLJussi Pohjolainen
 
Bag Of Tricks From Iusethis
Bag Of Tricks From IusethisBag Of Tricks From Iusethis
Bag Of Tricks From IusethisMarcus Ramberg
 
MySQL Kitchen : spice up your everyday SQL queries
MySQL Kitchen : spice up your everyday SQL queriesMySQL Kitchen : spice up your everyday SQL queries
MySQL Kitchen : spice up your everyday SQL queriesDamien Seguy
 
Introdução ao Perl 6
Introdução ao Perl 6Introdução ao Perl 6
Introdução ao Perl 6garux
 
අරුණ හේරත්_MYSQL සිංහල_TL_I_033__techlogiclk.com.pdf
අරුණ හේරත්_MYSQL සිංහල_TL_I_033__techlogiclk.com.pdfඅරුණ හේරත්_MYSQL සිංහල_TL_I_033__techlogiclk.com.pdf
අරුණ හේරත්_MYSQL සිංහල_TL_I_033__techlogiclk.com.pdfAnilManage
 
Internationalizing CakePHP Applications
Internationalizing CakePHP ApplicationsInternationalizing CakePHP Applications
Internationalizing CakePHP ApplicationsPierre MARTIN
 
Heroku Waza 2013 Lessons Learned
Heroku Waza 2013 Lessons LearnedHeroku Waza 2013 Lessons Learned
Heroku Waza 2013 Lessons LearnedSimon Bagreev
 
How to write code you won't hate tomorrow
How to write code you won't hate tomorrowHow to write code you won't hate tomorrow
How to write code you won't hate tomorrowPete McFarlane
 
Digital Mayflower - Data Pilgrimage with the Drupal Migrate Module
Digital Mayflower - Data Pilgrimage with the Drupal Migrate ModuleDigital Mayflower - Data Pilgrimage with the Drupal Migrate Module
Digital Mayflower - Data Pilgrimage with the Drupal Migrate ModuleErich Beyrent
 
Coding Horrors
Coding HorrorsCoding Horrors
Coding HorrorsMark Baker
 
Devs for Leokz e 7Masters - WTF Oriented Programming
Devs for Leokz e 7Masters - WTF Oriented ProgrammingDevs for Leokz e 7Masters - WTF Oriented Programming
Devs for Leokz e 7Masters - WTF Oriented ProgrammingFabio Akita
 
Refactoring to Macros with Clojure
Refactoring to Macros with ClojureRefactoring to Macros with Clojure
Refactoring to Macros with ClojureDmitry Buzdin
 
テストデータどうしてますか?
テストデータどうしてますか?テストデータどうしてますか?
テストデータどうしてますか?Yuki Shibazaki
 

Similar to Coding Horrors (20)

Simple Ways To Be A Better Programmer (OSCON 2007)
Simple Ways To Be A Better Programmer (OSCON 2007)Simple Ways To Be A Better Programmer (OSCON 2007)
Simple Ways To Be A Better Programmer (OSCON 2007)
 
Short Intro to PHP and MySQL
Short Intro to PHP and MySQLShort Intro to PHP and MySQL
Short Intro to PHP and MySQL
 
Bag Of Tricks From Iusethis
Bag Of Tricks From IusethisBag Of Tricks From Iusethis
Bag Of Tricks From Iusethis
 
MySQL Kitchen : spice up your everyday SQL queries
MySQL Kitchen : spice up your everyday SQL queriesMySQL Kitchen : spice up your everyday SQL queries
MySQL Kitchen : spice up your everyday SQL queries
 
Introdução ao Perl 6
Introdução ao Perl 6Introdução ao Perl 6
Introdução ao Perl 6
 
අරුණ හේරත්_MYSQL සිංහල_TL_I_033__techlogiclk.com.pdf
අරුණ හේරත්_MYSQL සිංහල_TL_I_033__techlogiclk.com.pdfඅරුණ හේරත්_MYSQL සිංහල_TL_I_033__techlogiclk.com.pdf
අරුණ හේරත්_MYSQL සිංහල_TL_I_033__techlogiclk.com.pdf
 
Internationalizing CakePHP Applications
Internationalizing CakePHP ApplicationsInternationalizing CakePHP Applications
Internationalizing CakePHP Applications
 
Heroku Waza 2013 Lessons Learned
Heroku Waza 2013 Lessons LearnedHeroku Waza 2013 Lessons Learned
Heroku Waza 2013 Lessons Learned
 
R57.Php
R57.PhpR57.Php
R57.Php
 
Lithium Best
Lithium Best Lithium Best
Lithium Best
 
Php functions
Php functionsPhp functions
Php functions
 
How to write code you won't hate tomorrow
How to write code you won't hate tomorrowHow to write code you won't hate tomorrow
How to write code you won't hate tomorrow
 
Database api
Database apiDatabase api
Database api
 
Digital Mayflower - Data Pilgrimage with the Drupal Migrate Module
Digital Mayflower - Data Pilgrimage with the Drupal Migrate ModuleDigital Mayflower - Data Pilgrimage with the Drupal Migrate Module
Digital Mayflower - Data Pilgrimage with the Drupal Migrate Module
 
Coding Horrors
Coding HorrorsCoding Horrors
Coding Horrors
 
QA for PHP projects
QA for PHP projectsQA for PHP projects
QA for PHP projects
 
Devs for Leokz e 7Masters - WTF Oriented Programming
Devs for Leokz e 7Masters - WTF Oriented ProgrammingDevs for Leokz e 7Masters - WTF Oriented Programming
Devs for Leokz e 7Masters - WTF Oriented Programming
 
Refactoring to Macros with Clojure
Refactoring to Macros with ClojureRefactoring to Macros with Clojure
Refactoring to Macros with Clojure
 
テストデータどうしてますか?
テストデータどうしてますか?テストデータどうしてますか?
テストデータどうしてますか?
 
CakePHP workshop
CakePHP workshopCakePHP workshop
CakePHP workshop
 

More from Mark Baker

Looping the Loop with SPL Iterators
Looping the Loop with SPL IteratorsLooping the Loop with SPL Iterators
Looping the Loop with SPL IteratorsMark Baker
 
Looping the Loop with SPL Iterators
Looping the Loop with SPL IteratorsLooping the Loop with SPL Iterators
Looping the Loop with SPL IteratorsMark Baker
 
Looping the Loop with SPL Iterators
Looping the Loop with SPL IteratorsLooping the Loop with SPL Iterators
Looping the Loop with SPL IteratorsMark Baker
 
Deploying Straight to Production
Deploying Straight to ProductionDeploying Straight to Production
Deploying Straight to ProductionMark Baker
 
Deploying Straight to Production
Deploying Straight to ProductionDeploying Straight to Production
Deploying Straight to ProductionMark Baker
 
Deploying Straight to Production
Deploying Straight to ProductionDeploying Straight to Production
Deploying Straight to ProductionMark Baker
 
A Brief History of Elephpants
A Brief History of ElephpantsA Brief History of Elephpants
A Brief History of ElephpantsMark Baker
 
Aspects of love slideshare
Aspects of love slideshareAspects of love slideshare
Aspects of love slideshareMark Baker
 
Does the SPL still have any relevance in the Brave New World of PHP7?
Does the SPL still have any relevance in the Brave New World of PHP7?Does the SPL still have any relevance in the Brave New World of PHP7?
Does the SPL still have any relevance in the Brave New World of PHP7?Mark Baker
 
A Brief History of ElePHPants
A Brief History of ElePHPantsA Brief History of ElePHPants
A Brief History of ElePHPantsMark Baker
 
Anonymous classes2
Anonymous classes2Anonymous classes2
Anonymous classes2Mark Baker
 
Testing the Untestable
Testing the UntestableTesting the Untestable
Testing the UntestableMark Baker
 
Anonymous Classes: Behind the Mask
Anonymous Classes: Behind the MaskAnonymous Classes: Behind the Mask
Anonymous Classes: Behind the MaskMark Baker
 
Does the SPL still have any relevance in the Brave New World of PHP7?
Does the SPL still have any relevance in the Brave New World of PHP7?Does the SPL still have any relevance in the Brave New World of PHP7?
Does the SPL still have any relevance in the Brave New World of PHP7?Mark Baker
 
Does the SPL still have any relevance in the Brave New World of PHP7?
Does the SPL still have any relevance in the Brave New World of PHP7?Does the SPL still have any relevance in the Brave New World of PHP7?
Does the SPL still have any relevance in the Brave New World of PHP7?Mark Baker
 
Giving birth to an ElePHPant
Giving birth to an ElePHPantGiving birth to an ElePHPant
Giving birth to an ElePHPantMark Baker
 
A Functional Guide to Cat Herding with PHP Generators
A Functional Guide to Cat Herding with PHP GeneratorsA Functional Guide to Cat Herding with PHP Generators
A Functional Guide to Cat Herding with PHP GeneratorsMark Baker
 
A Functional Guide to Cat Herding with PHP Generators
A Functional Guide to Cat Herding with PHP GeneratorsA Functional Guide to Cat Herding with PHP Generators
A Functional Guide to Cat Herding with PHP GeneratorsMark Baker
 
SPL - The Undiscovered Library - PHPBarcelona 2015
SPL - The Undiscovered Library - PHPBarcelona 2015SPL - The Undiscovered Library - PHPBarcelona 2015
SPL - The Undiscovered Library - PHPBarcelona 2015Mark Baker
 
Zephir - A Wind of Change for writing PHP extensions
Zephir - A Wind of Change for writing PHP extensionsZephir - A Wind of Change for writing PHP extensions
Zephir - A Wind of Change for writing PHP extensionsMark Baker
 

More from Mark Baker (20)

Looping the Loop with SPL Iterators
Looping the Loop with SPL IteratorsLooping the Loop with SPL Iterators
Looping the Loop with SPL Iterators
 
Looping the Loop with SPL Iterators
Looping the Loop with SPL IteratorsLooping the Loop with SPL Iterators
Looping the Loop with SPL Iterators
 
Looping the Loop with SPL Iterators
Looping the Loop with SPL IteratorsLooping the Loop with SPL Iterators
Looping the Loop with SPL Iterators
 
Deploying Straight to Production
Deploying Straight to ProductionDeploying Straight to Production
Deploying Straight to Production
 
Deploying Straight to Production
Deploying Straight to ProductionDeploying Straight to Production
Deploying Straight to Production
 
Deploying Straight to Production
Deploying Straight to ProductionDeploying Straight to Production
Deploying Straight to Production
 
A Brief History of Elephpants
A Brief History of ElephpantsA Brief History of Elephpants
A Brief History of Elephpants
 
Aspects of love slideshare
Aspects of love slideshareAspects of love slideshare
Aspects of love slideshare
 
Does the SPL still have any relevance in the Brave New World of PHP7?
Does the SPL still have any relevance in the Brave New World of PHP7?Does the SPL still have any relevance in the Brave New World of PHP7?
Does the SPL still have any relevance in the Brave New World of PHP7?
 
A Brief History of ElePHPants
A Brief History of ElePHPantsA Brief History of ElePHPants
A Brief History of ElePHPants
 
Anonymous classes2
Anonymous classes2Anonymous classes2
Anonymous classes2
 
Testing the Untestable
Testing the UntestableTesting the Untestable
Testing the Untestable
 
Anonymous Classes: Behind the Mask
Anonymous Classes: Behind the MaskAnonymous Classes: Behind the Mask
Anonymous Classes: Behind the Mask
 
Does the SPL still have any relevance in the Brave New World of PHP7?
Does the SPL still have any relevance in the Brave New World of PHP7?Does the SPL still have any relevance in the Brave New World of PHP7?
Does the SPL still have any relevance in the Brave New World of PHP7?
 
Does the SPL still have any relevance in the Brave New World of PHP7?
Does the SPL still have any relevance in the Brave New World of PHP7?Does the SPL still have any relevance in the Brave New World of PHP7?
Does the SPL still have any relevance in the Brave New World of PHP7?
 
Giving birth to an ElePHPant
Giving birth to an ElePHPantGiving birth to an ElePHPant
Giving birth to an ElePHPant
 
A Functional Guide to Cat Herding with PHP Generators
A Functional Guide to Cat Herding with PHP GeneratorsA Functional Guide to Cat Herding with PHP Generators
A Functional Guide to Cat Herding with PHP Generators
 
A Functional Guide to Cat Herding with PHP Generators
A Functional Guide to Cat Herding with PHP GeneratorsA Functional Guide to Cat Herding with PHP Generators
A Functional Guide to Cat Herding with PHP Generators
 
SPL - The Undiscovered Library - PHPBarcelona 2015
SPL - The Undiscovered Library - PHPBarcelona 2015SPL - The Undiscovered Library - PHPBarcelona 2015
SPL - The Undiscovered Library - PHPBarcelona 2015
 
Zephir - A Wind of Change for writing PHP extensions
Zephir - A Wind of Change for writing PHP extensionsZephir - A Wind of Change for writing PHP extensions
Zephir - A Wind of Change for writing PHP extensions
 

Recently uploaded

Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...OnePlan Solutions
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...MyIntelliSource, Inc.
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comFatema Valibhai
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software DevelopersVinodh Ram
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...soniya singh
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackVICTOR MAESTRE RAMIREZ
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerThousandEyes
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...kellynguyen01
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfkalichargn70th171
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providermohitmore19
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsArshad QA
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...harshavardhanraghave
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideChristina Lin
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsAndolasoft Inc
 
Project Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationProject Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationkaushalgiri8080
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdfWave PLM
 
why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfjoe51371421
 

Recently uploaded (20)

Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
 
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
 
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS LiveVip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.com
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software Developers
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStack
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview Questions
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.js
 
Project Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationProject Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanation
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf
 
why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdf
 
Exploring iOS App Development: Simplifying the Process
Exploring iOS App Development: Simplifying the ProcessExploring iOS App Development: Simplifying the Process
Exploring iOS App Development: Simplifying the Process
 

Coding Horrors

  • 1. Coding Horrors A Horror Film Fan’s Guide to PHP Coding Nightmares
  • 4. Coding Horrors – Friday the 13th
  • 5. Coding Horrors – Friday the 13th // 3 month appointments $date_to = date('d/m/Y'); $date_from = date("d/m/Y", strtotime(" +3 months")); $get = "select * from appointments where date between '$date_from' and '$date_to'"; $get_connect = mysqli_query($con2, $get); $get_rows = mysqli_num_rows($get_connect);
  • 6. Coding Horrors – Friday the 13th mysql> select * from appointments; +----+-----------+------------------------+ | id | date | appointment | +----+-----------+------------------------+ | 1 | 2/1/2018 | PHPNW January meetup | | 2 | 6/2/2018 | PHPNW February Meetup | | 3 | 6/3/2018 | PHPNW March Meetup | | 4 | 3/4/2018 | PHPNW April Meetup | | 5 | 1/5/2018 | PHPNW May Meetup | | 6 | 5/6/2018 | PHPNW June Meetup | | 7 | 3/7/2018 | PHPNW July Meetup | | 8 | 7/8/2018 | PHPNW August Meetup | | 9 | 4/9/2018 | PHPNW September Meetup | | 10 | 2/10/2018 | PHPNW October Meetup | | 11 | 6/11/2018 | PHPNW November Meetup | | 12 | 4/12/2018 | PHPNW December Meetup | +----+-----------+------------------------+ 12 rows in set (0.01 sec) mysql> select * from appointments -> where date between '2/1/2018' and '5/1/2018’; +----+-----------+------------------------+ | id | date | appointment | +----+-----------+------------------------+ | 1 | 2/1/2018 | PHPNW January meetup | | 4 | 3/4/2018 | PHPNW April Meetup | | 7 | 3/7/2018 | PHPNW July Meetup | | 9 | 4/9/2018 | PHPNW September Meetup | | 10 | 2/10/2018 | PHPNW October Meetup | | 12 | 4/12/2018 | PHPNW December Meetup | +----+-----------+------------------------+ 6 rows in set (0.00 sec)
  • 9. 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))); }
  • 10. 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;
  • 11. Coding Horrors – The Ring SELECT s.id, s.name, c.name FROM students s LEFT JOIN classes c ON c.studentid = s.id GROUP BY s.id, s.name ORDER BY s.name;
  • 13. Coding Horrors – The Mummy
  • 14. Coding Horrors – The Mummy $Data = $connection->prepare( "SELECT * FROM :TableName ORDER BY :OrderByColumn :OrderDirection LIMIT 10 OFFSET :Offset" ); $Data->bindValue(':TableName', $TableName, PDO::PARAM_STR); $Data->bindValue(':Offset', $Offset, PDO::PARAM_INT); $Data->bindValue(':OrderByColumn', $OrderColumn, PDO::PARAM_STR); $Data->bindValue(':OrderDirection', $OrderDirection, PDO::PARAM_STR); $Data->execute();
  • 16. Coding Horrors – Night of the Living Dead
  • 17. Coding Horrors – Night of the Living Dead function getAddressDetails() { $data->name = 'Mark'; // $data->address = 'mi casa'; // $data->city = 'Manchester'; // $data->country = 'UK'; // return json_encode($data, JSON_UNESCAPED_UNICODE); return json_encode($data); }
  • 18. Coding Horrors – Night of the Living Dead function getAddressDetails() { $data->name = 'Mark’; return json_encode($data); } git commit -m "Remove address, which is now accessed through a separate API call"
  • 19. Coding Horrors – Night of the Living Dead function addValues($a, $b, $options) { $sum = 0; return $a + $b; }
  • 20. Coding Horrors – Night of the Living Dead function squareIt($bar) { return $bar ** 2; $bar *= $bar; return $bar; }
  • 22. Coding Horrors – The Ring (US Remake)
  • 23. Coding Horrors – The Ring (US Remake) DRY Don’t Repeat Yourself
  • 25. Coding Horrors – Chain Letter
  • 26. Coding Horrors – Chain Letter mysql> select * from users; +----+----------+----------------------------------+-----------+ | id | username | password | followers | +----+----------+----------------------------------+-----------+ | 1 | Shirley | 5f4dcc3b5aa765d61d8327deb882cf99 | 8,6,10 | | 2 | Deborah | d1133275ee2118be63a577af759fc052 | 4 | | 3 | Julie | 25d55ad283aa400af464c76d713c07ad | 4,5 | | 4 | Jane | 4297f44b13955235245b2497399d7a93 | 3,5 | | 5 | Jennifer | d8578edf8458ce06fbc5bb76a58c5ca4 | 3,4 | | 6 | Alison | 827ccb0eea8a706c4c34a16891f84e7b | 9,1 | | 7 | Philipa | 8621ffdbc5698829397d97767ac13db3 | NULL | | 8 | Sue | acc6f2779b808637d04c71e3d8360eeb | 1 | | 9 | Annabel | 0d107d09f5bbe40cade3de5c71e9e9b7 | 6 | | 10 | Cathy | 5badcaf789d3d1d09794d8f021f40f0e | 3,1 | +----+----------+----------------------------------+-----------+ 10 rows in set (0.00 sec)
  • 27. Coding Horrors – Chain Letter mysql> select * from users where followers like '%1%'; +----+----------+----------------------------------+-----------+ | id | username | password | followers | +----+----------+----------------------------------+-----------+ | 1 | Shirley | 5f4dcc3b5aa765d61d8327deb882cf99 | 8,6,10 | | 6 | Alison | 827ccb0eea8a706c4c34a16891f84e7b | 9,1 | | 8 | Sue | acc6f2779b808637d04c71e3d8360eeb | 1 | | 10 | Cathy | 5badcaf789d3d1d09794d8f021f40f0e | 3,1 | +----+----------+----------------------------------+-----------+ 4 rows in set (0.00 sec)
  • 28. Coding Horrors – Chain Letter mysql> select * from users where followers like '%,1,%'; Empty set (0.00 sec) mysql> select * from users -> where followers = '1' or followers like '1,%' or followers like '%,1,%' or followers like '%,1'; +----+----------+----------------------------------+---------+ | id | username | password | friends | +----+----------+----------------------------------+---------+ | 6 | Alison | 827ccb0eea8a706c4c34a16891f84e7b | 9,1 | | 8 | Sue | acc6f2779b808637d04c71e3d8360eeb | 1 | | 10 | Cathy | 5badcaf789d3d1d09794d8f021f40f0e | 3,1 | +----+----------+----------------------------------+---------+ 3 rows in set (0.00 sec)
  • 29. Coding Horrors – Chain Letter mysql> select * from users where find_in_set(1,friends); +----+----------+----------------------------------+---------+ | id | username | password | friends | +----+----------+----------------------------------+---------+ | 6 | Alison | 827ccb0eea8a706c4c34a16891f84e7b | 9,1 | | 8 | Sue | acc6f2779b808637d04c71e3d8360eeb | 1 | | 10 | Cathy | 5badcaf789d3d1d09794d8f021f40f0e | 3,1 | +----+----------+----------------------------------+---------+ 3 rows in set (0.00 sec)
  • 31. Coding Horrors – The Mummy Returns
  • 32. 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();
  • 33. 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();
  • 34. 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();
  • 35. 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();
  • 37. Coding Horrors – Halloween
  • 38. Coding Horrors – Halloween function monthList() { $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(monthList() as $date) { echo $date->format("M Y") . PHP_EOL; }
  • 39. 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
  • 40. Coding Horrors – Halloween function monthList() { $startDate = new DateTime('first day of this month'); $endDate = (clone $startDate) ->add(new DateInterval('P1Y')); $dateRange = new DatePeriod($startDate, new DateInterval('P1M'), $endDate); foreach($dateRange as $date) { yield $date; } } foreach(monthList() as $date) { echo $date->format("M Y") . PHP_EOL; }
  • 43. Coding Horrors – Alien if (strpos(" " . $fileURL, "s3://") >= 1) { // Do something }
  • 45. Coding Horrors – Alien if (strpos($fileURL, "s3://") !== false) { // Do something }
  • 48. Coding Horrors – Se7en $calendar = [ 01 => 'January', 02 => 'February', 03 => 'March', 04 => 'April', 05 => 'May', 06 => 'June', 07 => 'July', 08 => 'August', 09 => 'September', 10 => 'October', 11 => 'November', 12 => 'December', ];
  • 49. 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
  • 50. Coding Horrors – Se7en $calendar = [ 1 => 'January', 2 => 'February', 3 => 'March', 4 => 'April', 5 => 'May', 6 => 'June', 7 => 'July', 8 => 'August', 9 => 'September', 10 => 'October', 11 => 'November', 12 => 'December', ];
  • 52. Coding Horrors – Pan’s Labyrinth
  • 53. Coding Horrors – Pan’s Labyrinth public function __construct( MagentoFrameworkModelContext $context, MagentoFrameworkViewDesignInterface $design, MagentoFrameworkRegistry $registry, MagentoStoreModelAppEmulation $appEmulation, MagentoStoreModelStoreManagerInterface $storeManager, MagentoFrameworkAppRequestInterface $request, MagentoNewsletterModelTemplateFilter $filter, MagentoFrameworkAppConfigScopeConfigInterface $scopeConfig, MagentoNewsletterModelTemplateFactory $templateFactory, MagentoFrameworkFilterFilterManager $filterManager, array $data = [] ) { parent::__construct($context, $design, $registry, $appEmulation, $storeManager, $data); $this->_storeManager = $storeManager; $this->_request = $request; $this->_filter = $filter; $this->_scopeConfig = $scopeConfig; $this->_templateFactory = $templateFactory; $this->_filterManager = $filterManager; }
  • 54. Coding Horrors – Pan’s Labyrinth The ideal number of arguments for a function is zero (niladic). Next comes one (monadic) followed closely by two (dyadic). Three arguments (triadic) should be avoided where possible. More than three (polyadic) requires very special justification—and then shouldn't be used anyway. Bob Martin – “Clean Code”
  • 57. Coding Horrors – Scream $sql = "SELECT MAX(id) AS max_page FROM videos"; $result = @$conn->query($sql); $row = @mysql_fetch_array($result); echo $row["max_page"];
  • 58. Coding Horrors – Scream @dns_get_record(); dns_get_record();
  • 59. Coding Horrors – Scream Warning: dns_get_record() expects at least 1 parameter, 0 given in %filename% on %line%
  • 60. Coding Horrors – Scream set_error_handler(function($errno, $errstr, $errfile, $errline, array $errcontext) { if (error_reporting() === 0) { // error was suppressed with the @-operator throw new ErrorException( 'WHY ARE YOU SUPPRESSING ERRORS RATHER THAN HANDLING THEM!' ); // return false; } throw new ErrorException($errstr, 0, $errno, $errfile, $errline); });
  • 61. Coding Horrors – Scream try { @dns_get_record(); } catch (ErrorException $e) { echo 'EXCEPTION: ', $e->getMessage(), PHP_EOL; } try { dns_get_record(); } catch (ErrorException $e) { echo 'EXCEPTION: ', $e->getMessage(), PHP_EOL; }
  • 62. Coding Horrors – Scream EXCEPTION: WHY ARE YOU SUPPRESSING ERRORS RATHER THAN HANDLING THEM? EXCEPTION: dns_get_record() expects at least 1 parameter, 0 given
  • 64. Coding Horrors – Scream • Scream • PECL Extensions • Disables the @ error control operator so that all errors are reported. ini_set('scream.enabled', true);
  • 66. Coding Horrors – Dawn of the Dead
  • 67. 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 }
  • 70. Who am I? Mark Baker 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

Editor's Notes

  1. Friday the 13th
  2. Using varchar for dates in a database table
  3. Varchar fields for dates/times in a database, especially when using d/m/Y or m/d/Y formats for storage
  4. Can work when using Y-m-d format Please use appropriate date datatypes
  5. The Ring (original Japanese version, not the US remake)
  6. Unnecessary (and expensive) loops
  7. Single database query to achieve the same result
  8. Single database query to achieve the same result
  9. The Mummy (1999 version)
  10. Misunderstood bindings
  11. Night of the Living Dead
  12. 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 out.
  13. 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 out.
  14. Static Analysis tools should pick these up Don’t rely on PHP’s opcache optimisation to clean up the redundant code, it still impairs readability, and won’t clean up the unused but mandatory $options argument
  15. Static Analysis tools should pick this up too
  16. The Ring (US Remake)
  17. There are times when you simply shouldn’t try to copy a great original
  18. Chain Letter
  19. A real turkey
  20. Besides the md5 hashed passwords Comma-separated list of ids in a column
  21. Common approach
  22. Common approach
  23. This is so common that MySQL even provides a “syntactic sugar” function to help But it still can’t use indexes and entails a full table scan Better yet, normalize your table structures
  24. The Mummy Returns
  25. More binding misunderstandings
  26. Can’t duplicate the same named parameter
  27. Can’t duplicate the same named parameter
  28. Halloween
  29. 31st of October
  30. 31st of October
  31. Alien
  32. This code actually works without issue;
  33. BUT the PHP Docs are pretty explicit here....
  34. 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.
  35. Se7en
  36. Seven is the highest digit in octal notation
  37. Gives a "Parse error: Invalid numeric literal" in PHP7.... What do you mean you're not running php7 yet?
  38. Gives a "Parse error: Invalid numeric literal" in PHP7.... What do you mean you're not running php7 yet?
  39. Gives a "Parse error: Invalid numeric literal" in PHP7.... What do you mean you're not running php7 yet?
  40. Pan’s Labyrinth
  41. Scream
  42. Besides the inefficiency of suppressing the errors, this also hides problems
  43. Besides the inefficiency of suppressing the errors, this also hides problems Error handling is still called, but with error reporting temporarily set to 0
  44. Only the unsuppressed call display a warning
  45. If we set an error handler, we can process that warning cleanly, converting it to a catchable Exception if we want
  46. Besides the inefficiency of suppressing the errors, this also hides problems
  47. Besides the inefficiency of suppressing the errors, this also hides problems
  48. Besides the inefficiency of suppressing the errors, this also hides problems
  49. Dawn of the Dead
  50. Docblocks are no longer in synch with the code Building the docs should highlight this mismatch
  51. Don’t let these code smells come back to haunt you again and again
  52. But exorcise them when you find them