PHP probleem met oefening

Status
Niet open voor verdere reacties.

marvy

Gebruiker
Lid geworden
25 dec 2000
Berichten
396
De volgende code werkt dus niet ( letterlijk gecopieerd uit ebook )

<?php

echo "Hello there. So I hear you're learning to be a PHP programmer!\n";
echo "Why don't you type in your name for me:\n";
$name = trim(fgets(STDIN));

echo "\nThanks, " . $name . ", it's really nice to meet you.\n\n";

?>


Geeft alsresultaat:

Hello there. So I hear you're learning to be a PHP programmer! Why don't you type in your name for me:
( ! ) Notice: Use of undefined constant STDIN - assumed 'STDIN' in C:\wamp\www\index.php on line 5
Call Stack
# Time Memory Function Location
1 0.0005 669368 {main}( ) ..\index.php:0
( ! ) Warning: fgets() expects parameter 1 to be resource, string given in C:\wamp\www\index.php on line 5
Call Stack
# Time Memory Function Location
1 0.0005 669368 {main}( ) ..\index.php:0
2 0.0007 669712 fgets ( ) ..\index.php:5

Thanks, , it's really nice to meet you.
 
Gegoogled, dan vind ik dit

An already opened stream to stdin. This saves opening it with
Code:
<?php
$stdin = fopen('php://stdin', 'r');
?>
If you want to read single line from stdin, you can use
Code:
<?php
$line = trim(fgets(STDIN)); // reads one line from STDIN
fscanf(STDIN, "%d\n", $number); // reads number from STDIN
?>
 
De code werkt wel, maar niet op een website. Daar werkt input anders (via een tekstveld bijv.). Op de command-line werkt het script prima:
frank@darkstar:~$ php /tmp/stdin.php
Hello there. So I hear you're learning to be a PHP programmer!
Why don't you type in your name for me:
ikke

Thanks, ikke, it's really nice to meet you.
 
Status
Niet open voor verdere reacties.
Terug
Bovenaan Onderaan