C++ to Php
Moderator: General Moderators
C++ to Php
I'm having trouble with things I would consider basic code in PHP. I've worked with C++ but never with the web based languages and I'm trying to figure out how to write the same types of programs(console based) I was using in C++ . HTML's easy to pick up but in PHP I'm having trouble even finding a replacement for things like CIN. I can post some graphics and such but as far as making my programs accept data from a user and actually do something I'm dead in the water. Can you even do that sort of interactive programming in PHP?
- Jonah Bron
- DevNet Master
- Posts: 2764
- Joined: Thu Mar 15, 2007 6:28 pm
- Location: Redding, California
Re: C++ to Php
Data is accepted from the user as Post or Get HTTP requests. These values passed over HTTP can be accessed through the $_GET and $_POST superglobal variables.
http://www.w3schools.com/php/php_get.asp
http://www.w3schools.com/php/php_get.asp
Re: C++ to Php
You mean stdin?bobtech12 wrote:I'm having trouble even finding a replacement for things like CIN.
Code: Select all
<?php
function prompt($question, $accepted = array(), $variants = array())
{
do {
fwrite(STDOUT, $question . ': ');
foreach ($variants as $id => $name) {
fwrite(STDOUT, "\n [$id] $name");
}
if (!empty($variants)) {
fwrite(STDOUT, "\n");
}
$answer = trim(fgets(STDIN, 1024));
if (empty($accepted) || in_array($answer, $accepted)) {
break;
}
} while (true);
return $answer;
}
$age = prompt("Enter your age");
$sex = strtolower(prompt("Enter your sex", array("m", "f", "M", "F", 0), array("m" => "Male", "f" => "Female", 0 => "Not sure")));
// .... etc
Re: C++ to Php
By CIN I mean as in cin << a;
Just a very simple console input really. That's why it's really bothering me that I can't get it. Isn't allowing a basic text based input a staple of most languages or am I overly naive?
Just a very simple console input really. That's why it's really bothering me that I can't get it. Isn't allowing a basic text based input a staple of most languages or am I overly naive?
Re: C++ to Php
Not naive, just not really attentivebobtech12 wrote:Isn't allowing a basic text based input a staple of most languages or am I overly naive?