Skip to content

Instantly share code, notes, and snippets.

@theUniC
Created December 1, 2011 15:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save theUniC/1417486 to your computer and use it in GitHub Desktop.
Save theUniC/1417486 to your computer and use it in GitHub Desktop.
PHP Best Practices - engineering.emagister.com
# Se debe cambiar debidamente el path al directorio dónde PHP
# escanea en busca de archivos ini.
pecl install apc
echo "extension=apc.so" >> /etc/php5/conf.d/apc.ini
<?php
$string = ' Gracias por venir!';
printf('Hola a todos' . $string); // Lento
echo 'Hola a todos' , $string; // Rápido
<?php
$myDb = new PDO(...);
function getRowSet()
{
global $myDb;
return $myDb->query('SELECT ...');
}
function getAnotherRowSet()
{
global $myDb;
$myDb = new PDO(...);
return $myDb->query('SELECT ...');
}
$rowSet = getRowSet();
$anotherRowSet = getAnotherRowSet();
$thirdRowSet = getRowSet();
<?php
if (array_key_exists('submit-btn', $_POST)) {
// Vulnerabilidad muy muy obvia
// a modo de ejemplo
$firstName = $_POST['firstname'];
$path = __DIR__ . "/user/profiles/$firstname.txt";
@readfile($path);
}
<?php
class Registry extends ArrayObject
{
private $_instance;
private function __construct() {}
public function __clone()
{
throw new Exception('This object cannot be clonned!');
}
public static function getInstance()
{
if (null === static::$_instance) {
static::$_instance = new static();
}
return static::$_instance;
}
}
Registry::getInstance()->offsetSet('test', 'stringTest');
<?php
$string = 'Hola a todos!';
if (strpos($string, 'H')) {
$string .= ' Gracias por venir!';
}
echo $string; // Imprimirá "Hola a todos!"
<?php
$string = 'Hola a todos!';
if (false !== strpos($string, 'H')) {
$string .= ' Gracias por venir!';
}
echo $string; // Imprimirá "Hola a todos! Gracias por venir!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment