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
}
Related
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.
I've updated a website and found an annoying problem in my pages
I resolved it, but i'm not realy convinced it should look like this.
To me i'm not an PHP expert its strange behaviour could someone explain me whats going on.
I had this code:
if($Menu == "index"){
{if ($Language == "UK"){echo "<td><h1>Welcome</h1>";}
{if ($Language == "NL"){echo "<td><h1>Welkom </h1>";}
}
else
// if $menu was not index it displayed a hyperlink to Welcome page
The string thing here that while the checking for language worked fine.
The comparison for $Menu did not work, even if it contained the word index
As the whole page was generated on the fly and some string operations where done before
I assumed that maybe, despite i also tested it with
echo "dump Menu variable " . $Menu
Which resulted in jut displaying the word index on the page. So maybe there would go something wrong in string types or something like that
So i experimented with
if($Menu === "index")
No luck
Well i finally solved it like this
if (strpos($Menu,'index' !==false)
Is that really the way it should be done???, I don't feel really comfortable with it.
As to me its strange that for $Language it just works as it should (in my opinion).
Is there is some type problem here, or maybe unwanted endings \n could i perhaps ehm normalize the string to do a content of readable string comparions or, a different type of equal operator. As it feels to me as $Menu could be handled more easily. Maybe a reformat or but i'm not sure here.
Looks indeed like your index-String is containing whitespaces, instead of strpos you could use trim( $Menu ) to get rid of them.
But the best would be to prevent their occurence. You could try echo "dump Menu variable |" . $Menu ."|"; or just var_dump( $Menu ) to identify the additional characters.
Maybe you could post, your code-segment where $Menu is filled.
(sorry can't just comment)
You can try something like this
$Menu = trim($Menu);
$Language = trim($Language);
$langAllowed = ["UK", "NL"]; // zero element is default;
if(!in_array($Language, $langAllowed )) $Language = $langAllowed[0];
$menuLocales=[
"index" => [
"UK"=>"Welcome",
"NL"=>"Welkom "
],
"default"=>[
"UK"=>"Welcome default (no index)",
"NL"=>"Welkom default (no index)"
]
];
$MenuIndex = isset($menuLocales[ $Menu ]) ? $Menu : "default";
echo "<td><h1>".$menuLocales[ $MenuIndex ][ $Language ]."</h1>";
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.
another simple thing that's got me stuck:
I'm using the following to check on the current url and select a div class dependent on the result:
$checkit = $_SERVER['PHP_SELF'];
...
<li "; if(strstr($checkit,'welcome')) { echo "class='active_tab'"; }...
What I want to be able to do is also check if the url includes other words which would also require that same 'li' item to be given the 'active_tab' class, but i can't figure out the format. Something like this, although obviously this doesn't work:
<li "; if(strstr($checkit,'welcome', 'home', 'yourprofile')) { echo "class='active_tab'"; }...
Can someone help?
I know there's a better way but stop-gap fix would be:
$searchStrings = array('welcome','home','yourprofile');
$stringFound = false;
foreach($searchStrings as $checkString)
{
if(strstr($checkit, $checkString))
{
$stringFound = true;
break;
}
}
Then use $stringFound to change your output.
Edit 1: Switched continuefor break thanks ZombieHunter (It's late -_-)
Edit 2: Alternatively you can use a regular expression (though I think that's overkill here)
if(preg_match('/(welcome|home|your profile)/',$checkit))
{
// Do your stuff here
}
But this is not as expressive (easier to read and extend an array) and if those values start piling up its easier to hook the array into some storage like DB query.
I am writing a php function for wordpress that is executed through an XML feed. Therefore we are excepting a feed and then based on the nodes placing those in our website. What I need help with is we have a bunch of different images of credentials (i.e BBB, chamber of commerce etc) What I need therefore is when there is a link to a BBB then it should display a picture, if not then it should be blank. The problem I am running into is because the BBB links will be random based on different businesses. Any help would be greatly appreciated. Thanks.
If URL "pic"
else "no pic"
Do you mean this? Otherwise please explain your problem better.
if (!empty($url)) {
echo '<img src="' .$url. '" />';
}
else {
echo ' ';
}
Check here when empty returns false (and therefore !empty is true) and really consider if this fits your needs.
Maybe I'm missing something, but wouldn't this do?
if($license1) { print "<img src=\"/path/to/bbb.logo\" alt=\"BBB Logo\" />"; }
A method would be creating an array
like $feeds = array("pic","xml");
then testing if its in array like
if in_array($url,$feeds)
// your code;
or the second method would be creating an temp var like $tmp = $url =="pic" ? "pic" : "nopic";
or to set just an boolean $tmp = $url =="pic" ? TRUE : FALSE;
then you can test it like this
if($url) // if its == "pic" it would return true otherwise false
//make your url
Also a shorthand way is to do
But when the given var is an array I think you need to use is_array(), or if it's a class objec,t use is_object() to verify that it has content.