PHP & MySQL
The following are some useful references for you to refresh your PHP & MySQL knowledge:
- PHP 8 Tutorial by W3Schools: A complete guide and tutorials to learn the latest PHP language.
- PHP Tutorial: Another resource to learn the PHP language.
- Clean Code concepts adapted for PHP: To learn some of the best practices and clean coding standards in PHP.
- PHP: Namespaces: To learn about PHP namespaces.
- PHP: Language Reference: Use this reference to find any topic related to the PHP language.
- MySQL Database: To learn about the MySQL Database.
- MySQL Tutorial: Another good resource to learn about the MySQL Database.
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
PDOandmysqliextensions in PHP. - What functions are available in PHP for regex?
Quizzes