The following are some useful references for you to refresh your PHP & MySQL knowledge.
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";
}
?>
PDO
and mysqli
extensions in PHP.