As questioned above I'm trying to get user input in my PHP script to do further tasks. Is there any method in PHP to get user input like 'scanf()' used in C
<?php
echo "Swapping Numbers </br> Please select the method: </br>";
echo "1. Using 3rd variable </br> 2. Using add/sub </br> 3. Using mul/div";
//read user input $choice
switch($choice)
{
case 1:
break;
case 2:
break;
case 3:
break;
default:
echo "</br>You entered wrong choice";
}
?>
the STDIN constant is pre-defined and available for you to use immediately.
You can get user input using fgets and fscanf
<?php
$line = trim(fgets(STDIN)); // reads one line from STDIN
fscanf(STDIN, "%d\n", $number); // reads number from STDIN
For example
<?php
echo "Enter a number: ";
fscanf(STDIN, "%d\n", $number);
switch ($number) {
case 1: echo "one\n"; break;
case 2: echo "two\n"; break;
case 3: echo "three\n"; break;
default: echo "I can't count that high\n";
}
Check out the I/O docs for more detailed information
You should use readline() :
var_dump(readline('Enter number: '));
You can handle user input from form element or from url.
in your case you can use like this.
$choice = $_GET['choise'];
User will type like this
http://localhost/your_file_name.php?choise=option
than you should use your switch
switch($choice)
{
case 1:
break;
case 2:
break;
case 3:
break;
default:
echo "</br>You entered wrong choice";
}
?>
Related
I've made two files php, one of the has the validation php code and another has the form where I input some data, but looks something is wrong with Switch command, look many time to find the problem but unfortunately I did not get true.
Here is my code;
First file where I've FORM
<form action="Validation_search_02.php" method="post">
<label>Your favourite fruit:<input type="text" name="t_Opst" id="t_Opst"></label>
<input type="submit" name="G_mby" id="G_mby">
</form>
The second file where I've the validation file(php)
<?php
if(isset($_P0ST["G_mby"])){
$L_ings=$_POST["t_Opst"];
switch($L_ings){
case "Apple":
echo "Your favourite fruit is $L_ings";
break;
case "Pear":
echo "Your favourite fruit is $L_ings";
break;
case "Banana":
echo "Your favourite fruit is $L_ings";
break;
case "Mango":
echo "Your favourite fruit is $L_ings";
break;
default:
echo "You should eat some fruit";
}
}
?>
In your php file you can verify if there is a POST request, when it comes to your code there is no need for a switch because there is a repetition of the same code several times, you can use an if and or clauses instead. Try this:
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST"){
$L_ings = $_POST["t_Opst"];
echo "hola";
if($L_ings == "Pear" or $L_ings == "Banana" or $L_ings == "Mango" or $L_ings == "Apple")
echo "Your favourite fruit is $L_ings";
else
echo "You should eat some fruit";
}
?>
I'm trying to use the steam web API to show the current status of ONE steam user if he is (online,away,offline,looking to trade) etc and have setup a PHP switch as follows:
<?
$status = $steamprofile['personastate'];
switch($status) {
case 0:
echo "offline!";
break;
case 1:
echo "online!";
break;
case 2:
echo "Busy.";
break;
case 3:
echo "Away.";
break;
case 4:
echo "Snooze.";
break;
case 5:
echo "Looking to trade.";
break;
case 6:
echo "Looking to play.";
break;
default:
echo "Unknown status";
}
echo $status;
?>
It works great but only shows the user status when logged unto the site and not simply when the user is online/away/offline etc on the steam platform.
I'm wondering if anyone know how to fix so it displays the current user status on steam, not just if he/she is logged in on the website.
This question already has answers here:
Reference - What does this error mean in PHP?
(38 answers)
Closed 8 years ago.
I have an issue with my PHP web application that involves the use of headers throughout my program files. To be more specific, I created a file called "error.php" that looks like this:
<p><?php
$errorCode= $_GET['message'];
switch($errorCode)
{
case "1":
echo"<br>Sorry, all input fileds except for apartment number must be completed.<br>";
break;
case "2":
echo "<br>You have entered an invalid name format.<br>";
break;
case "3":
echo "<br>Name update not successful.<br>";
break;
case "4":
echo "<br>Phone number update not successful.<br>";
break;
case "5":
echo "<br>You have entered an invalid phone number format.<br>";
break;
case "6":
echo "<br>You have entered an invalid street addres format.<br>";
break;
case "7":
echo "<br>Apartment number update not successful.<br>";
break;
case "8":
echo "<br>City name update not successful.<br>";
break;
case "9":
echo "<br>State name update not successful.<br>";
break;
case "10":
echo "<br>Zip code update not successful.<br>";
break;
case "11":
echo "<br>Email address update not successful.<br>";
break;
case "12":
echo "<br>You have entered an invalid email address format.<br>";
break;
case "13":
echo "<br>You have entered an invalid password format.<br>";
break;
case "14":
echo "<br>Can not find what to update.<br>";
break;
case "15":
echo "<br>You have entered an invalid apartment number.<br>";
break;
case "16":
echo "<br>You have entered an invalid password format.<br>";
break;
case "17":
echo "<br>You have entered an invalid email address format.<br>";
break;
case "18":
echo "<br>User not found in the database.<br>";
break;
case "19":
echo "<br>User ID not found in database. Login Failure.<br>";
break;
case "20":
echo "<br>Failed connection to the database.<br>";
break;
case "21":
echo "<br>Street Address update not successful.<br>";
break;
case "22":
echo "<br>User already exists.<br>";
break;
case "23":
echo "<br>Failed to create a new user account.<br>";
break;
case "24":
echo "<br>You have entered an invalid address format.<br>";
break;
default:
echo "<br>You have an unknown error.<br>";
}
?> </p>
THE PROBLEM is that somewhere else in the application I would use statements like the following:
header("location:/view/error.php?message=23");
header("location:/view/error.php?message=21"); and so on...
I now come to realize this is problematic because I constantly get Warnings like,
"Warning: Cannot modify header information - headers already sent by (output started at /home/content/47/12002447/html/model/userRegistrationAction.php:6)/home/content/47/12002447/html/model/userRegistrationAction.php on line 92"
And I get plenty of them mostly pointing to the same file. My QUESTION is, is there a way that I can direct pages to "error.php" in a way similar to the way I am currently using but with using header?
I do not want to use "include("error.php?message= 'some #')" because it introduces a ton of overhead.
I would like to thank you guys in advance for any help
make sure you call the 'header' function BEFORE any output. In other words, no HTML or javascript can be called earlier than the 'header' function or it will fail with the error message you describe. as a good rule, put all 'header' function calls above the 'html' open tag in your files
I know how to code a solution to my problem, I am just interested in the shortest way to code it. Thats the problem:
I am parsing a URL, the parsing can have 3 results, local, staging or production. I figure out which one it is using a regex. I do that in a function called getServer().
Now in that function I return an array which has 3 elements which are either 0 or 1. If the first is 1 and the other two are 0, it means, its a local server for example.
Now when I get that array back I still have to write an if to see which of the array elems is 1.
if($returnArrayFromFunction[0] == '1') {
// do the stuff for the local server case
}
if($returnArrayFromFunction[1] == '1') {
// do the stuff for the staging server case
}
if($returnArrayFromFunction[2] == '1') {
// do the stuff for the production server case
}
Is there some way to shorten that code?
Thanks for your time!
Don't return an array with the index of the value 1 encoding the result. Define three constants
define('SERVER_LOCAL', 1);
define('SERVER_STAGING', 2);
define('SERVER_PRODUCTION', 4);
And return the correct constant from your code, then use a switch statement
switch($serverType) {
case(SERVER_LOCAL)::
DoStuffForLocalServer();
break;
case ...
If terseness is your goal, shorten the switch to
$serverType == SERVER_LOCAL ? doLocal() : $serverType == SERVER_STAGING ? doStaging() : doProduction();
Is as short as it gets, but will probanly be frowned upon :-)
Why not just return an id number in getServer()?
$serverId = getServer();
switch ($serverId) {
case 0: // Local
// Code
break;
case 1: // Staging
// Code
break;
case 2: // Production
// Code
}
Note:
If you will need to use these server ids elsewhere in the code, it may be easier to keep track of which id corresponds to which server by using a naming convention with define(). This can also make your code easier to read and thus easier to debug.
define('SERVER_LOCAL', 0);
define('SERVER_STAGING', 1);
define('SERVER_PRODUCTION', 2);
Then you can replace the above switch with the following:
switch ($serverId) {
case SERVER_LOCAL:
// Code
break;
case SERVER_STAGING:
// Code
break;
case SERVER_PRODUCTION:
// Code
}
Try this code
$case=array_search(1,$returnArrayFromFunction);
switch($case)
{
0:
break;
// do the stuff for the local server case
1:
break;
// do the stuff for the staging server case
2:
// do the stuff for the production server case
break;
}
Try like
foreach($returnArrayFromFunction as $key=>$return)
{
if($return == '1')
{
switch($key)
{
case '0' : //Stuff at local
break;
case '1' : //Stuff at Staging
break;
case '2' : //Stuff at production
break;
}
}
}
$code = join('', $returnArrayFromFunction );
//something like '111' / '010' / '110'
switch( $code ){
case '111':
//all values 1
break;
case '001':
//something else
break;
}
Your getServer() function should just return one value instead of array. And then use following code:
$serverId = getServer();
switch ($serverId) {
case 0: // local server
// Your code
break;
case 1: // testing server
// Your code
break;
case 2: // live server
// Your code
}
I write a script for a alerter service. I used php connection_status function to get connection status. Because the script require a inf loop. I look some example and try other scripts but connection_status function always return 0. I think i tried all the script ways. Please help me.
<?php
ignore_user_abort (TRUE);
$x=0;
while ($x++ < 20) {
print $x;
sleep (1);
}
switch (connection_status ()) {
case CONNECTION_NORMAL:
$status = 'Normal';
break;
case CONNECTION_ABORTED:
$status = 'User Abort';
break;
case CONNECTION_TIMEOUT:
$status = 'Max Execution Time exceeded';
break;
case (CONNECTION_ABORTED & CONNECTION_TIMEOUT):
$status = 'Aborted and Timed Out';
break;
default:
$status = 'Unknown';
break;
}
file_put_contents('test.txt',$status);
?>
My system;
php-5.3.1-1 and apache-2.2.14-1
this works... I tested to my space, works if user close windows, but if user press esc button don't work, I really don't know why...
<?php
ignore_user_abort (TRUE);
$x=0;
while (1) {
echo "\n";
if (connection_status()!=0){
file_put_contents('test.txt',connection_status());
die();
}
}
file_put_contents('test.txt',connection_status());
?>
The function connection_status return a int value...
try to add ob_implicit_flush(); in the head
Thanks for all answers! I find a way to solve problem. May be output buffer is the reason for this problem. I run flush(),ob_flush() functions after output and solve the problem! Again thanks alot.
<?php
ignore_user_abort (TRUE);
$x=0;
while ($x++ < 10) {
print " ";
flush();
ob_flush();
sleep (1);
}
switch (connection_status ()) {
case CONNECTION_NORMAL:
$status = 'Normal';
break;
case CONNECTION_ABORTED:
$status = 'User Abort';
break;
case CONNECTION_TIMEOUT:
$status = 'Max Execution Time exceeded';
break;
case (CONNECTION_ABORTED & CONNECTION_TIMEOUT):
$status = 'Aborted and Timed Out';
break;
default:
$status = 'Unknown';
break;
}
file_put_contents('testa.txt',$status);
?>
Nothing of the above worked for me, so I wrote this:
https://bitbucket.org/sivann/coolagent/raw/7d024bfff4a7ef3ce90dff14d4953b3347dfeaa2/server/app/src/Netstat.php which actually detects connection status from OS. Linux only.