0
0
PHPprogramming~10 mins

Comparison with long-running servers (Node.js) in PHP - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to print a message in PHP.

PHP
<?php
 echo [1];
?>
Drag options to blanks, or click blank then click option'
A"Hello, world!"
Bprint("Hello, world!")
CHello, world!
Decho Hello, world!
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting quotes around the string
Using print() inside echo
2fill in blank
medium

Complete the code to start a PHP built-in server on port 8000.

PHP
php -S [1]
Drag options to blanks, or click blank then click option'
A127.0.0.1:9000
Blocalhost:8080
C0.0.0.0:8000
Dlocalhost:8000
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong port number
Using IP instead of localhost
3fill in blank
hard

Fix the error in the PHP code that tries to keep a long-running server alive.

PHP
<?php
while (true) {
    [1]("Server running...\n");
    sleep(5);
}
?>
Drag options to blanks, or click blank then click option'
Aprint
Becho
Cconsole.log
Dprintf
Attempts:
3 left
💡 Hint
Common Mistakes
Using JavaScript functions like console.log
Using print without parentheses
4fill in blank
hard

Fill both blanks to create a PHP script that listens on port 8080 and outputs a response.

PHP
<?php
$address = '127.0.0.1';
$port = [1];
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_bind($socket, $address, $port);
socket_listen($socket);

$client = socket_accept($socket);
socket_write($client, [2]);
socket_close($client);
socket_close($socket);
?>
Drag options to blanks, or click blank then click option'
A8080
B"Hello from PHP server!"
C80
D"Welcome!"
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong port number
Not quoting the message string
5fill in blank
hard

Fill all three blanks to create a PHP script that accepts a connection, reads data, and sends a response.

PHP
<?php
$address = '0.0.0.0';
$port = [1];
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_bind($socket, $address, $port);
socket_listen($socket);

$client = socket_accept($socket);
$data = socket_read($client, [2]);
socket_write($client, [3]);
socket_close($client);
socket_close($socket);
?>
Drag options to blanks, or click blank then click option'
A9000
B1024
C"Data received"
D512
Attempts:
3 left
💡 Hint
Common Mistakes
Using too small or too large read size
Not quoting the response string