I have this code but is not working and I don't know why...
function link_next($local_id, $link_type) {
if ( !is_numeric($local_id) ) die();
elseif ($link_type = 'a-href') { $something = 'a-href'; }
elseif ($link_type = 'link-rel') { $something = 'link-rel'; }
else { $something = 'blablabla'; }
return $local_id.'-'.$something;
}
$id ='14678';
echo link_next($id, 'link-rel');
// edit
The function always display 14678-a-href
you are using =
in condition checking. You need to use ==
or ===
(strict comparison)
Try this:
function link_next($local_id, $link_type) {
if ( !is_numeric($local_id) ){
die();
} elseif ($link_type == 'a-href') {
$something = 'a-href';
} elseif ($link_type == 'link-rel') {
$something = 'link-rel';
} else {
$something = 'blablabla';
}
return $local_id.'-'.$something;
}
I just corrected the fact that there was no {} on the first check, not sure if that will matter but hey, worth a try.
You are using the assignment operator instead of the equivalence operator. Aka you're using = instead of ==.
You can use assignment in an if, but then you're saying "Assign the variable the value and then the condition is true if the variable is not empty." (where empty is 0, false, '', null, etc.)
EDIT: Got ninja'd. It's because I am more verbose, I guess.
You have to use two equal signs:
elseif ($link_type == 'a-href') { $something = 'a-href'; }
elseif ($link_type == 'link-rel') { $something = 'link-rel'; }
Otherwise you set $link_type
to the specified string!
You're using the assignment operator =
rather than the comparison operator ==
, or ===
(strict).
Also indenting your code properly helps make it easy for you to spot out errors.
function link_next($local_id, $link_type) {
if ( !is_numeric($local_id) ){
die();
}
elseif ($link_type == 'a-href') {
$something = 'a-href';
}
elseif ($link_type == 'link-rel') {
$something = 'link-rel';
}
else {
$something == 'blablabla';
}
return $local_id.'-'.$something;
}
$id ='14678';
echo link_next($id, 'link-rel');
echo link_next($id, 'link-rel');
You have three problems:
=
) instead of equality (==
) to compare the strings - this is what's breaking the code--
function link_next($local_id, $link_type) {
// early exit
if (!is_numeric($local_id)) die();
if ($link_type == 'a-href') {
$something = 'a-href';
} elseif ($link_type == 'link-rel') {
$something == 'link-rel';
} else {
$something = 'blablabla';
}
return $local_id.'-'.$something;
}
Your code is wrong, You are using '=' operator in if-else conditions instead of '==' operator. This will assign value to link type. You need to change your code:
function link_next($local_id, $link_type) {
if (!is_numeric($local_id))
return false;
if ($link_type == 'a-href') {
$something = 'a-href';
} elseif ($link_type == 'link-rel') {
$something == 'link-rel';
} else {
$something = 'blablabla';
}
return $local_id.'-'.$something;
}
Hope this helps