This question already has answers here:
The 3 different equals
(5 answers)
Closed 4 years ago.
I've been learning PHP for over a year now and it's always the little things that seem to go disastrously wrong, or I just forget what I'm doing.
I've been using Do-While loops in a few projects but recently one I created just didn't work. Once I numbed it down to almost nothing I noticed it just isn't stopping according to user input.
do {
echo "Hi there\n";
echo "Echo\n";
$userInput = readline();
} while ($userInput = 'continue');
echo "Exit";
I don't understand what's going wrong but something is. From my understanding the program will echo twice, listen for the user's input and loop through again while the user types continue - if not then will echo Exit. What am I doing wrong? This is such a simple task and it's annoying me. All the other topics I've searched for don't seem to be helping.
You need to use
while ($userInput == 'continue');
instead of
while ($userInput = 'continue');
because you have used wrong operator (= [assign] instead of == [equals])
Related
This question already has answers here:
PHP: Best way to check if input is a valid number?
(6 answers)
Closed 2 years ago.
So I have been trying to bring back to life my very old website.
I started with replacing ereg with preg, but it's been a very long time since I have written any PHP.
At the moment I am stuck on this:
$_POST['amount'] = preg_replace("/[^0-9/]",'',$_POST['amount']);
$_POST['amount'] = round($_POST['amount']);
if (!preg_match('/[^0-9]/', $_POST['amount'])) {
echo "Invalid amount.";
}else {
echo "Passed";
}
I'm not entirely sure where I am going wrong. Should it be !preg_match or preg_match for example?
Edit:
$_POST['amount'] allows a user to enter a number and needs to replace anything else other than a number if attempted.
Not sure what the error is (i.e. what is the current output and what was expected). However, one mistake in the prompt is that the pattern for the first preg_replace appears to have a typo: /[^0-9/] should probably be /[^0-9]/
preg_replace("/[^0-9]/",'',$_POST['amount']);
The rest looks like correct syntax (i.e. preg_match returns 0 if there is no match).
This question already has answers here:
PHP If statement always firing as true
(2 answers)
Closed 5 years ago.
I'm having issues with AND/OR in an if/else PHP statement. This is one of the codes in particular:
if(is_page_template('page-home.php') || ('zaadvokati.php')) {
$class_trans = 'class="trans-color standard-menu"';
}else{
$class_trans = 'class="not-page-home standard-menu"';
}
If it's like this - with || / OR , it loads every паге template with the first class_trans option and not just the 2 templates specified, and if you put AND instead of OR, then it loads just the first one ('page-home.php') with the first class_trans and doesn't load the second one ('zaadvokati.php') with it. I suppose that, for some reason, it doesn't recognize the second template, but I don't know why.
Can you help?
Thanks!
You need to reiterate the function in the condition. try this:
if(is_page_template('page-home.php') || is_page_template('zaadvokati.php')) {
$class_trans = 'class="trans-color standard-menu"';
}else{
$class_trans = 'class="not-page-home standard-menu"';
}
This question already has answers here:
Is GOTO in PHP evil? [closed]
(11 answers)
Closed 8 years ago.
I know goto statements are bad, but I find that it is hard to not use them when programming. I am a new programmer, and I know they are bad practice, but what are some ways to get around using them? I know about IF ELSE statements, but what other tools are out there to help me avoid using GoTo's?
Goto is a feature new, since PHP 5.3. There is some very, very specific reasons to use them. You will find some Goto's in drivers or kernel codes, but I really can't see any reason to use it in usual CMS, Blog, Social Network, e-shop and so on...
If you will return a 404, you can use a "header('errorpage.php')", for instance. You can assign a flag.
} elseif ($event['response']->getStatusCode() == 404) {
$errorno = '404';
//goto a;
$thingsGoingWrong = true;
}
.
.
<?php if ($thingsGoingWrong) { doBazinga(); } ?>
Considering you are not using a prior version of PHP, there are some rules for Goto, like you can't enter in a loop, or switch statement.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
The 3 different equals
Can anyone tell me why, when using the code below, I am getting redirected to elephant.com rather than seeing a 'giraffe!
<?php
$foo="giraffe";
if($foo="elephant"){
header("location:http://www.elephant.com");
exit();
}else{
echo $foo;}
?>
Thanks for looking
J
if($foo="elephant")
You're assigning $foo here, rather than comparing it; you should be doing:
if($foo=="elephant")
The result of an assignment operation is the value that's just been assigned; in this case, 'elephant' is evaluating to true.
Your if() statement has a single equal sign. This doesn't do a comparison in PHP; it sets the value and returns true.
In order to do a comparison, you need to use either a double-equal or a triple-equal sign:
if($foo == "elephant") { .... }
or
if($foo === "elephant") { .... }
The difference between the two is that double-equal doesn't care about the variable's data type, whereas triple-equal does. In this case, there's not much difference between them, but it's worth learning and understanding the differences because they can bite you if you don't know them. More info here: http://php.net/manual/en/language.operators.comparison.php
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
echo inside if loop
I trying to code a trading system and i have a list of entry and exit strategies. To lessen the number of lines in the code I planned to put all my strategies into an array for each entry and exit. My array is like this
$enter_strats = array(
array('name'=>"macd",'strat'=>"/$divergence[/$key]>0.1;"),
);
I am including the conditional statements inside the array as above.
while I am looping thru everyday prices, I have to check for each entry strategy if they they are true. My if statement is like this
foreach($divergence as $key=>$value)
{
if($trade ==0)
{
foreach($enter_strats as $k =>$v)
{
$strat = $v['strat'];
$strat = str_replace("#","$",$strat);
eval("\$strat = \"$strat\";");
if ($strat)
{
$trade =1;
$book->save($key,$close[$key],$v['name']);
}
}
}
}
The problem since it is a string its always if is always evaluating it to true. I tried to put one more eval inside if but its of no use.
Please help to solve this problem, its is very essential. Thanks a lot.
That's because you're trying to lessen the number of lines in the code.
Arrays intended to keep data, not code.
As soon as you understand it, your code will be okay.
'strat' should contain only data. An operator and a number for instance.
keeping variable name in the string makes no sense.
especially if you have this variable already.
You have already have $divergence[$key] in your code.
So, 'strat' should be just array('>',0.1)