eddioot Posted November 26, 2012 Share Posted November 26, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/271190-help-with-function-if-statement/ Share on other sites More sharing options...
Christian F. Posted November 26, 2012 Share Posted November 26, 2012 (edited) = 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 November 26, 2012 by Christian F. Quote Link to comment https://forums.phpfreaks.com/topic/271190-help-with-function-if-statement/#findComment-1395149 Share on other sites More sharing options...
eddioot Posted November 26, 2012 Author Share Posted November 26, 2012 Wow it really was that simple Thank you very much! Quote Link to comment https://forums.phpfreaks.com/topic/271190-help-with-function-if-statement/#findComment-1395156 Share on other sites More sharing options...
Christian F. Posted November 26, 2012 Share Posted November 26, 2012 You're welcome. Quote Link to comment https://forums.phpfreaks.com/topic/271190-help-with-function-if-statement/#findComment-1395161 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.