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)
Related
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])
This question already has answers here:
Checking if array is multidimensional or not?
(16 answers)
Closed 9 years ago.
How come when I run this code, I get an output of I am a multidimensional array! (the first block). I thought it would go into the second block, but it doesn't. What am I missing here?
$values = array('1','2');
if(isset($values[0][0])){
echo "I am a multidimensional array!";
}else{
echo "I am not a multidimensional array.";
}
$values = array(1,array(1,2));
$multi = false;
if(is_array($values)){
foreach($values as $k=>$v){
if(is_array($v)){
$multi = true;
break;
}
}
}
echo $multi ? "multi" : "not multi";
Try this:
if(is_array($values[0]))
Edit: This will check the first element of the array only. You should loop through each element to check if its truly multidimensional.
This code checks to see if the first element of the array is also an array. isset just checks whether or not a variable is NULL.
isset in your example is not working as expected. Perhaps there is a slight difference in functionality between PHP versions or setups. I didn't see anything in the manual but maybe you can:
http://php.net/manual/en/function.isset.php
Using is_array is more semantic, so in my opinion is a much better choice.
This code only goes into the if-branch for me, if the first value in the array is explicitly declared as a string,
$values = array('1',2);
– and with that the behavior is nothing but logical, because $values[0] is that text literal '1', and that has a first character that can be access using a zero based index.
So I guess either your real data is of a string type – or it maybe depends in the PHP version (I tested under 5.3.16).
Anyway, using is_array as the other answers already suggested is the right way to go here.
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:
Parse error on explode('-','foo-bar')[0] (for instance)
In PHP there are functions that return an array, for example:
$a = parse_url("https://stackoverflow.com/q/9461027/87015");
echo $a["host"]; // stackoverflow.com
My question is how to combine the above two statements into a single statement. This (is something that works in JavaScript but) does not work:
echo parse_url("https://stackoverflow.com/q/9461027/87015")["host"];
Note: function array dereferencing is available since PHP 5.4; the above code works as-is.
You can do a little trick instead, write a simple function
function readArr($array, $index) {
return $array[$index];
}
Then use it like this
echo readArr(parse_url("http://stackoverflow.com/questions/9458303/how-can-i-change-the-color-white-from-a-uiimage-to-transparent"),"host");
Honestly, the best way of writing your above code is:
$a = parse_url("http://stackoverflow.com/q/9461027/87015");
echo $a["scheme"];
echo $a["host"];
Isn't that what I originally posted?
Yes. Depending on context you may want a better name than $a (perhaps $url), but that is the best way to write it. Adding a function is not an attractive option because when you revisit your code or when someone reads your code, they have to find the obscure function and figure out why on earth it exists. Leave it in the native language in this case.
Alternate code:
You can, however, combine the echo statements:
$a = parse_url("http://stackoverflow.com/q/9461027/87015");
echo $a['scheme'], $a['host'];
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
php explode and array index
I am new to PHP and would like to ask if there is a way to
write a php code like
$lengthofstay = ($duration <= 0 ? 'optional' : explode("_", $duration )[0]);
i mean calling the explode function and at the sametime reading the first element of resulting array.
it would be possible in 5.4
but your code is as ugly as hell.
there is nothing good in writing all the code in one line.
Write in in three lines and when you come across it in a week, you wouldn't stumble upon it, puzzled.
if ($duration <= 0)
$lengthofstay = 'optional';
} else {
list($lengthofstay) = explode("_", $duration, 1);
}
nothing wrong with it.
if you want to make it strictly one-liner - create a function. and then call it
$lengthofstay = get_length_of_stay($duration);
it's shorter than your cunning construct and way more readable
You might want to use the limit-parameter from explode:
explode("_", $duration, 1)