Jump to content

Recommended Posts

Hi all,

 

i am fairly new with php and i am running into a problem with a php function. It is very basic but i've got something wrong here.

 

It is a function in wordpress that needs to analyze the post and extract the category. For every category another value needs to be returned. My code is:

 

function postimage($post_id){
$category = get_the_category( $post_id );
$id = $category[0]->cat_ID;


if ( $id = 45 ){
 $link = 'This is category 45';
}
else {
 $link = 'This is not category 45 ';
}


return $link;
}

 

There are 2 strange things happening:

 

1. The first statement is always true. Even if the category id is not 45 it still returns the first $link.

2. If i return the variable $id after the if statement it returns als 45. If i do it without the if statement it returns correct.

 

The 2 problems are obviously related but i don't understand what i'm doing wrong. Please help :tease-03:

Link to comment
https://forums.phpfreaks.com/topic/271190-help-with-function-if-statement/
Share on other sites

= is assignment, while == is for testing equality. What you're doing is assigning 45 to $id, and then testing if the returned value of the assignment (45) is equal to true (which it is).

Edited by Christian F.
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.