PHP
September 22 2023
if shortcut – no braces If your if statement has only one directive, like the following: if ($x) { echo “This directive”; } You can drop the braces: if ($x) echo “This directive”;
if-else shortcut You're probably used to the long, formal version of the if-else statement in PHP: if ($x == 5) { $y = 3 } else { $y = 2 } While there is nothing wrong with this statement, it's more efficient to use the shortened version, using the ternary operator: ($x == 5) ? $y = 3 : $y = 2;
echo shortcut echo has a shortcut too, so long as the shortcut for the PHP tag itself is supported on the server. This is particularly useful if you are echoing out small snippets as variables. So rather than: You have chosen widgets. You can use: You have chosen =$foo?> widgets.
SQL injection // Escape the name. $sql['name'] = mysql_real_escape_string($clean['name']); // Construct the query. $query = "SELECT * FROM users WHERE name = '{$sql['name']}'"; or using // Provide the query format. $query = $db->prepare('SELECT * FROM users WHERE name = :name'); // Provide the query data and execute the query. $query->execute(array('name' => $clean['name']));
Comparison Operators - String if (strpos($authors, 'Chris') !== FALSE) { echo 'Chris is an author.'; } else { echo 'Chris is not an author.'; }
Use Isset() Instead Of Strlen() if (isset($username[5])) { // The username is at least six characters long. }
Voluptates Corporis Placeat
January 20 2019 - 34 CommentsLorem ipsum dolor sit amet, consectetur adipisicing elit. Eos saepe dolores et nostrum porro odit reprehenderit animi, est ratione fugit aspernatur ipsum. Nostrum placeat hic saepe voluptatum dicta ipsum beatae.
Dolorum Dolores Itaque
January 19 2019 - 64 CommentsLorem ipsum dolor sit amet, consectetur adipisicing elit. Eos saepe dolores et nostrum porro odit reprehenderit animi, est ratione fugit aspernatur ipsum. Nostrum placeat hic saepe voluptatum dicta ipsum beatae.
Quisquam Dignissimos
January 17 2019 - 93 CommentsLorem ipsum dolor sit amet, consectetur adipisicing elit. Eos saepe dolores et nostrum porro odit reprehenderit animi, est ratione fugit aspernatur ipsum. Nostrum placeat hic saepe voluptatum dicta ipsum beatae.
Voluptas Optio Soluta
January 15 2019 - 112 CommentsLorem ipsum dolor sit amet, consectetur adipisicing elit. Eos saepe dolores et nostrum porro odit reprehenderit animi, est ratione fugit aspernatur ipsum. Nostrum placeat hic saepe voluptatum dicta ipsum beatae.
Voluptates Corporis Placeat
January 14 2019 - 120 CommentsLorem ipsum dolor sit amet, consectetur adipisicing elit. Eos saepe dolores et nostrum porro odit reprehenderit animi, est ratione fugit aspernatur ipsum. Nostrum placeat hic saepe voluptatum dicta ipsum beatae.
Praesentium Asperiores
January 10 2019 - 143 CommentsLorem ipsum dolor sit amet, consectetur adipisicing elit. Eos saepe dolores et nostrum porro odit reprehenderit animi, est ratione fugit aspernatur ipsum. Nostrum placeat hic saepe voluptatum dicta ipsum beatae.