Unexpected behaviour using the PHP logical operator &&

I have a recipe website and when a contributor cancels his/her account, I want to keep their recipes and display them as the recipes of an anonymous user. If I do an inner join on users.id = recipes.user_id then the recipe name of the deleted user doesn’t show up and that could be used as a solution however I would prefer to keep the recipes and just display them as from anonymous. I tried various redundant versions of !isset() and empty() which didn’t work. In order to find a solution, I started with the simple process of showing the name “anonymous” for all recipe user_ids equal to one and recipe category_id equal to one. There is only one recipe with both category_id and recipe user_id equal to one however my code printed out all of the user ids equal to one and not just the one with category id equal to one.

foreach ($users as $user):

if ($user->id == $recipe->user_id){

echo $this->Html->link($user->contributor, ['controller' => 'bios', 'action' => 'view', $recipe->user_id]); 

} elseif ( $recipe->user_id == '1' && $recipe->category_id == '1' ) {

	echo "Anonymous";

endforeach; //users

If a Recipe always belongs to one user but could be anonymous, then it sounds to me like your user_id on the recipes table needs to be nullable.

Then you can delete that user (or just set that user_id on the recipe entity to null) and cake will give you an empty user property if you fetch

$recipesWithUsers = $this->Recipes->find()->contain(['Users']);

Do you have many users with id=1? If yes then it works fine.
If you have doubt about && operator, change whole condition in if() to true, then false and check results. Maybe problem is not the && operator.

About deleting users, beside what @KevinPfeifer wrote about null, you can hard code anonymous user in your database as let’s say id=1 (or some easy to remember number), name “anonymous”, other data empty or n/a or whatever and when you deleting someone, just update his posts user_id to 1.

Thanks KevinPfeifer and jarekgol for your input.