I have code as bellow :
number_format(($hasilz->harga>100000 ? $hasilz->harga+2000 :
($hasilz->harga>300000 ? $hasilz->harga+4000 :
($hasilz->harga>400000 ? $hasilz->harga+8000 :
$hasilz->harga+10000))), 0, ',', '.')
this code result and read +2000 and +10000 only
any idea ?
Let's see what you got there (simplified):
if (hagara > 100000) {
harga+2000
} else if (hagara > 300000) {
hagara+4000
} else if (hagara > 400000) {
hagara+8000
} else {
hagara+10000
}
If you write it like this, its easy to see. hagara is either >100000 which results in +2000 or it is less, then it results in +10000.
In other words, the two else if will never be true, because if they were, the first if would already be true.
I think this is also a good example, when you should NOT use the tenary operator. It just makes it really hard to read and understand... sometimes the good old it-approach ist just the better solution. ;)
EDIT: To answer the question, you have to use a different order of the if-statements, beginning with the biggest one (or write them completely different). However, as already mentioned, you shouldn't do that.
Related
I used to code with Groovy ... I found that this 'feature' (no idea what they called it) so fun n nice (I heard they implemented this on C# too).
for example ... I want to display person neighbour name
I just type
println person?.neigbour?.name ;
it means if the neighbour is empty / blank .. it didn't display anything .
how to do this in php 5/yii?
example:
instead of typing long codes like
'/>
Would it be better to type like
'/>
In the first place I think this is no Yii issue, but simply PHP. Assuming, that you use Yii with nice models, there it would go sth like this:
if($person && $person->neighbour && !empty($person->neighbour->name)) {
echo $person->neighbour->name;
}
a shortcut for this may be (not so nice):
echo $person ? ($person->neighbour ? ($person->neighbour->name ? $person->neighbour->name : "" ) : "" ) : "";
use empty()
check:
if(!empty($variable))
{
//show fields here
}
I have been running into a weird issue for some time with php now and wonder if anybody can help. It's probably a totally minor stupid thing I don't see.
"if" statements don't seem to work for me when I use && and || for logical expressions.
Latest example:
$isNotSet = !isset($moved); // var_dump prints false
$moveSuccess = $moved instanceof SuccessNotification; // var_dump prints true
if(($isNotSet == true) || ($moveSuccess == true)){
the script always breaks at exactly that line and doesn't go any further. It won't execute anything after this.
I have tried lots of things, e.g.
if(!isset($moved) || $moved instanceof SuccessNotification){
or
if(!isset($moved) === true || $moved instanceof SuccessNotification){
or
$isNotSet = !isset($moved);
$moveSuccess = $moved instanceof SuccessNotification;
if($isNotSet === true || $moveSuccess === true){
All don't work. I'm aware they all mean the same, I was just trying to eliminate the thought of being on the wrong track. Some of those examples are marked as syntax error in Netbeans, but not all of them, which got me thinking.
I'd like to know what's going on here because this limits coding a lot. But as I said, I probably overlook a totally simple thing.
edit: the script breaks without any errors or warnings shown, it just stops working at that line
edit2:
I took the snippet and executed it on its own.
$moved = new SuccessNotification($code, $title, $message);
echo 1;
$isNotSet = !isset($moved);
echo 2;
$moveSuccess = $moved instanceof SuccessNotification;
echo 3;
if(($isNotSet === true) || ($moveSuccess === true) || 1){
echo 4;
}else{
echo 5;
}
echo 6;
This prints "123", nothing else. And yes, I have included the SuccessNotification class (otherwise there would have been an error anyways) which is being used system-wide already and properly working, just removed that line for posting here.
I take it that nobody really has a clue and therefore tries to find a simple solution which is ok because I thought it's just a simple thing. But I realize I have tried everything you suggest or would do in this situation.
Shouldnt it be:
if(($isNotSet === true) || ($moveSuccess === true)){
Instead of:
if(($isNotSet == true) || ($moveSuccess == true)){
I got some trouble with some part of PHP code. It's part of a CMS' module (Hikashop/Joomla).
Here are the lines:
echo $this->payment->display('new_payment_method',
$row->order_payment_method,
$row->order_payment_id,false);
} else {
$text = JText::sprintf('PAY_WITH_X',$this->payment->
getName($row->order_payment_method,$row->order_payment_id));
for the part $row->order_payment_id I would like to have all rows but one. Is there a way to make it simple ???
Without testing the code (never played with Joomla), but the basic idea would be:
if( $row->order_payment_id != "unwanted_value_here" )
{
// do stuff here
}
That would be the logical approach.
I'm thinking about an application which recieves massive Data from the User.
But this data has to be verified in php:
f.e. $_GET['id'] has to be a number every Element of an array must be a value between a specific range.
so how efficient are functions like is_int($x) or isset($x) ?
Maybe if($x!=null) is faster or (int) x is causing same results.
What is the best way to handle Data that needs to get to Database quick but needs to be verified? Is there any difference in $_GET and $_POST in speed?
Maybe implementing a class doing that improves something?
Maybe for an more concret chance to answer, here a bit inefficent code:
if(isset($_GET["x"]) && $_GET["x"] > 0) { $x = $_GET["x"]; }
if(isset($_GET["y"]) && $_GET["y"] > 0) { $y = $_GET["y"]; }
if(isset($_GET["winWidth"]) && $_GET["winWidth"] > 0) { $winWidth = $_GET["winWidth"]; }
if(isset($_GET["winHeight"]) && $_GET["winHeight"] > 0) { $winHeight = $_GET["winHeight"]; }
if(isset($_GET["a"])) { $a = $_GET["a"]; }
UPDATE:
What about further security functions like:
mysql_real_escape_string($str);
or
stripslashes()
?
Please Stackoverflow, show me the magic :)
Harry
The fastest way to check if something has been posted and whether it's a positive integer:
if (isset($_POST['field']) && ctype_digit($_POST['field']))
mysql_real_escape_string is incredibly slow, compared to addslashes. However, it's definitely more secure.
However, it's normally not necessary to worry about all this too much. We're talking about billionths of a second here.
<?php echo isset($areas['footer']) ? $areas['footer'] : null; ?>
Any way to improve that?
Note that you are echoing and in false condition it would be null which does not have any effect. You could say like 'empty' or ' ' or 'not found' instead. Other alternative is to get the return value of isset:
$return = isset($areas['footer']) ? $areas['footer'] : null;
if ($return)
{
// $return contains footer info
}
else
{
// footer was not set :(
}
Depending on where $areas comes from it might be cleaner to assign it to a variable:
$footer = isset($areas['footer']) ? $areas['footer'] : null;
Then you can use $footer without any additional isset checks.
You can also spare the else branch, by setting a default:
$footer = null;
if (isset($areas['footer'])) {
$footer = $areas['footer'];
}
echo $footer;
No, this is the most concise way of handling this sort of output.
"i'm using this kind of code very often"
Maybe you should avoid the issue altogether by using a template language, or encapsulating this behavior in a function?
like this:
function get_area($area) {
if... //your code
return $area
One shorter version i can think of would be:
<?php !isset($areas['footer']) or print $areas['footer']; ?>
But i'm not sure if it is faster or more elegant.
What do you guys think?
echo $areas['footer'];
Simple and has the exact same effect as the original line.
Edit in reply to Felix
This gives a notice, but unless you're supposed to turn this in as ultra-perfect homework, it doesn't really matter. You can either fill your code with isset calls or ignore small stuff like this. I'd worry about it if I was working in say... Java, but pragmatically nobody is going to care if PHP code that works produces notices.