Symfony2 : how to fix the “Warning: SessionHandler::read()” Exception

If you too you got an error like this one :

Warning: SessionHandler::read(): open(\xampp\tmp\sess_hagg7jkmp7l7fmh5ec4k6jmgu0, O_RDWR) failed: No such file or directory (2) in [...]\app\cache\dev\classes.php line 423

I let you know that you just have to add/change these 2 lines in app/config/config.yml to fix it :

framework:
    session:
        handler_id: session.handler.native_file
        save_path: "%kernel.root_dir%/sessions"

PHP : short syntax (cheat sheet)

Whether you need them to shorten your code, to understand code from someone else, or to use them to write your templates, here is a list of short syntax you can use in PHP :

Output : echo

For templates :

<?= $myVar ?>

which is similar to :

<?php echo $myVar; ?>

Ternary operator (conditional operator)

The combination of question mark and colon represent a specific syntax reserved to ternary operators.

$myVar = (CONDITION) ? VALUE_1 : VALUE_2;

function $myFunction()
{
  return (CONDITION) ? VALUE_1 : VALUE_2;
}

which are respectively similar to :

if (CONDITION) $myVar = VALUE_1;
else $myVar = VALUE_2;

function $myFunction()
{
  if (CONDITION) return VALUE_1;
  else return VALUE_2;
}

Single conditional structures : with an output

This only works with print statement (because it actually always returns 1, thus == true).

(CONDITION) AND print(SOMETHING);

which is similar to :

if (CONDITION)
{
  print(SOMETHING);
}

Conditional structures : if … elseif … else

For pure PHP code :

if (CONDITION 1):
  INSTRUCTION 1;
  INSTRUCTION 2;
elseif (CONDITION 2):
  INSTRUCTION 3;
else:
  INSTRUCTION 3;
endif;

For templates :

<?php if (CONDITION 1): ?>
...
<?php elseif (CONDITION 2): ?>
...
<?php else: ?>
...
<?php endif ?>

Conditional structures : switch … case

For templates :

Also, becareful with any space between the switch and the first case : this is really not a good idea to close PHP tag et reopen it between them. FYI: I really don’t like switches within templates.

<?php switch($myVar):
case 1: ?>
...
<?php break;?>
<?php case 2: ?>
...
<?php break;?>
<?php endswitch;?>

Loop structures : for / foreach / while

For templates :

<?php for($i=0; $i<$i_max; $i++): ?>
...
<?php endfor ?>

<?php while (CONDITION): ?>
...
<?php endwhile ?>

<?php foreach($myArray as $value): ?>
...
<?php endforeach ?>

PHP : How to execute a MySQLi query after a multi_query (for PHP > 5.3) ?

I just got a problem today : I needed to execute a $mysqli->query() after a $mysqli->multi_query() and it didn’t work. Here is the error I got :

Fatal error: Call to a member function fetch_array() on a non-object in [...]

I then checked the PHP Documentation about mysqli::multi_query and tried to use the solution suggested by jcn50 but I got this error :

Strict Standards: mysqli::next_result(): There is no next result set. Please, call mysqli_more_results()/mysqli::more_results() to check whether to call this function/method in [...]

Because it only works for PHP <= 5.2.

I then find a solution that is logic :

$mysqli->multi_query("Many MySQL queries concatenated by a semicolon");

while ($mysqli->next_result()) // flush multi_queries
{
  if (!$mysqli->more_results()) break;
}

$mysqli->query("MySQL statement #1");
$mysqli->query("MySQL statement #2");
$mysqli->query("MySQL statement #3");