Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 10 months ago.
Improve this question
So my taslk is to program two numbers randomly (for example 0 and 1) and then replace the numbers with "Hello" and "bye". I've done the generating it rendomly part, but now I'm struggling with the replacing Part. It would be cool If the solution would be by using "if" and "else"
Here's what i've done so far,
thank you in advance
<?php
$zaehler = 0;
while($zaehler < 10)
{
echo mt_rand(0, 1);
if(0)
{
echo "bye" ;
}
else (1) ;
{
echo "hello" ;
}
$zaehler++;
}
?>
For your case, just use a comparison statement
e.g.
if($result==0) { // do something;} else {// do another thing; }
(the $result will only be either 0 or 1, so just use one if-then-else)
<?php
$zaehler = 0;
while($zaehler < 10){
$result=mt_rand(0, 1);
echo $result;
if($result==0) {
echo "bye" ;
} else {
echo "hello" ;
}
$zaehler++;
}
?>
So i just saw your post,
First, you need to assign mt_rand to a variable.
Afer your statement IF isnt correct, you need to evaluate something like this $i === 0
And about your else you need an else if, so you can take a look to this code ;)
$idx = 0;
while ($idx < 10) {
$random_int = mt_rand(0, 1);
if ($random_int === 0) {
echo "bye\r\n";
} else if ($random_int === 1) {
echo "hello\r\n";
}
$idx++;
}
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
If the phone number matches , I am able to print "Yes". If it doesnot match , i need to print "No". But if i include else part , its executing only else part. Please guide me how and where to write else part.
<?php if($re['phone_v'] == "1"){
echo "Yes";
}else if($re['phone_v'] == "0"){
for($i=0;$i < count($crusers);$i++) {
if ($re['phone'] == $crusers[$i]['phone']) {
echo "Yes";
}
}
}
?>
I am checking condition inside table in view. small thing is making complicated.
This will help you.
$re['phone_v'] = 0;
$re['phone'] = 1;
$crusers[0]['phone'] = 1;
if($re['phone_v'] == "1"){
echo "Yes";
}else{
if($re['phone_v'] == "0")
{
for($i=0;$i < count($crusers);$i++)
{
if ($re['phone'] == $crusers[$i]['phone'])
{
echo "Yes11";
}else{
echo "No";
}
}
}
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I want to make just simple incrementing with session but the session just stop every time on 2. Can you please help me what I should do?
if (!isset($_SESSION["current"]))
{
$_SESSION["current"] = 1;
}
$_SESSION["current"] = $_SESSION["current"] +1;
echo "SESSION: ".$_SESSION["current"]."<br>CURRENT: ";
I tryed this code and it doesnt work too:
<?php
if (isset($_POST["previous"]))
{
$_SESSION["current"] = $_SESSION["current"] - 1;
}
if (isset($_POST["next"]))
{
$_SESSION["current"] = $_SESSION["current"] + 1;
}
echo "SESSION: ".$_SESSION["current"]."<br>CURRENT: ";
?>
I believe you want to use this "current" value for pagination. If that is the case you are going about it a bit wrong. Remember the session will store the last value set until it is destroy or reset with another value. Here is my take
<?php
session_start(); // according to you, you don't need this so please remove
$current = 0; // lowest value for our pagination
if(isset($_SESSION['current'])){
$current = $_SESSION['current']; // session has an entry
}
if ((isset($_POST["previous"]) || isset($_POST['next'])))
{
if(isset($_POST['previous'])){
$current -= 1; // for previous
}else{
$current += 1; // for next
}
$current = ($current >= 0)?$current: 0; // reset to 0 when we get to negative
}else{
$current = 0; // we maybe back on the "start page"
}
$_SESSION['current'] = $current;
echo "SESSION: ".$_SESSION["current"]."<br>CURRENT: ";
That code should work, maybe your session is not active. try to issue this on top of your script:
if (session_status() == PHP_SESSION_NONE) {
session_start();
}`
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 4 years ago.
Improve this question
I need to make a conditional statement where the 5 variables should be empty before perform an action. The problem is some variables can be zero, false or NULL value. How to catch all of them?
Which one is better?
If (!$a && !$b && !$c && !$d && !$e) {
// do some action
} else {
exit;
}
OR
If (empty($a) && empty($b) && empty($c) && empty($d) && empty($e)) {
// do some action
} else {
exit;
}
Thank you.
First of all, use "code sample" to show code.
When you use !$a you are actually casting $a to boolean.
The problem here is
$a = 0;
if (!$a) {
echo 'NOT EMPTY';
} else {
echo 'EMPTY';
}
//OUTPUT EMPTY BECAUSE 0 to boolean is FALSE
Same example with NULL value.
About empty i advice you to check the documentation
Read it and choose the way that fit your needs.
Hope this helps
This could be an approach to check multiple variables are empty at once.
<?php
$a = $b = $c = $d = $e = '';
#$b = 5; // comment out to go on else block
if (empty($a . $b . $c . $d . $e)){
echo "All variables are empty, do what you want to do man";
}else{
echo "One of variable is not empty";
}
?>
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
Im working on a simple roll a dice program. So it has to generate a random number from 0-5 and when it selects 0-4 it echo's and when it selects 5 it echo's.
To it keeps echo the first echo 'you got 6'. I cant seem to get my mind around this.
<!DOCTYPE html>
<html>
<head>
<title>A loop of your own</title>
<link type='text/css' rel='stylesheet' href='style.css'/>
</head>
<body>
<?php
$trows = 0;
$dice = 0;
while ($dice == 0)
{
$dice = rand(0,5);
$trows++;
}
if ($dice = 5)
{
echo 'you got 6';
}
else
{
echo 'you got 1-5';
}
echo '<br><br>';
echo $trows;
?>
</body>
if ($dice = 5)
Is actually assigning the value of 5 to dice, rather than comparing the values, so the first part of the if statement will be entered, rather than the else.
if ($dice == 5)
is what you are looking for
Edit: From your comments I think this is what you are looking for
$trows = 0;
$dice = 0;
while ($dice != 5)
{
$dice = rand(0,5);
$trows++;
if ($dice == 5) {
echo 'you got 6';
} else {
echo 'you got 1-5';
}
echo '<br><br>';
echo $trows;
}
Your if statement is not testing for a condition, but rather performing an assignment and then evaluating the truth of that assignment (which will always be true in this case).
Your code should be:
if($dice == 5)
{
echo 'you got 1-5';
}
Notice the == instead of the single =
This will do the trick
while ($trows == 0)
{
$dice = rand(0,5);
if ($dice == 5)
{
echo 'you got 6';
}
else
{
echo 'you got 1-5';
$trows++;
}
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
I was just practicing and wanted to see if there was a more robust way to write this? Should I use a for loop?
<?php
function recursion($number){
if ($number === 0){
break;
}
else {
$subtracted_number = $number-1;
echo $subtracted_number;
recursion($subtracted_number);
}
}
recursion(9);
?>
//output is 876543210
as you can do it in a simple for loop you won't need your recursion here
function countdown($num)
{
for($i=$num-1; $i>0; $i--)
{
if($i !=0)
echo $i;
}
}
use as:
countdown(9);//echoes 87654321
function recursion($number){
$number--;
if ($number > 0){
echo $number;
recursion($number);
}
}
Just for fun http://codepad.org/4S4BcofG
function recursion($number) {
while ($number > 1) {
$number--;
echo $number;
}
}