Page 1 of 1

C++ to Php

Posted: Wed Oct 27, 2010 9:33 pm
by bobtech12
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?

Re: C++ to Php

Posted: Wed Oct 27, 2010 11:09 pm
by Jonah Bron
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

Re: C++ to Php

Posted: Wed Oct 27, 2010 11:16 pm
by Weirdan
bobtech12 wrote:I'm having trouble even finding a replacement for things like CIN.
You mean stdin?

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

Posted: Thu Oct 28, 2010 11:04 pm
by bobtech12
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?

Re: C++ to Php

Posted: Fri Oct 29, 2010 12:48 am
by Weirdan
bobtech12 wrote:Isn't allowing a basic text based input a staple of most languages or am I overly naive?
Not naive, just not really attentive :) . I gave you the example of working with stdin/stdout in my first reply.