PHP & MySQL

The following are some useful references for you to refresh your PHP & MySQL knowledge.

Try it out!

1. The following PHP code attempts to write a log message to a file. However, it doesn’t work as expected and is not writing anything to the file. Debug and try to solve the issue.

<?php
/**
 * Function to write a message to a log file
 * 
 * @param string $message The message to write to the log file
 * 
 * @return void
 */
function writeLog( $message ) {
    $file = fopen( 'log.txt', 'r');

    fwrite( $file, $message . PHP_EOL );
    fclose($file);
}

writeLog( 'This is a test log.' );
?>

2. Fix the below problem where we are allowed to pass in empty email values, but the email key should be there.

<?php
$data = array(
    'user' => array(
        'name'  => 'Alice',
        'email' => '',
    ),
);

if ( ! empty( $data['user']['email'] ) ) {
    echo 'Email key is provided.' . "\n";
} else {
    echo 'Email key is missing.' . "\n";
}
?>

Ideas to Explore

  • How does PHP handle type conversions during operations?
  • How does PHP handle session management? What functions are commonly used to start, modify, and destroy sessions?
  • Compare and contrast the PDO and mysqli extensions in PHP.
  • What functions are available in PHP for regex?