PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, January 8, 2022

[FIXED] String compare not working

 January 08, 2022     php, wordpress     No comments   

Issue

I am trying to compare post titles in wordpress to avoid creating a post with a title that already exists.

foreach ($postnamearray as $value) 
{
    if($value === $titelzor)
    {
        echo' '.$value.' === '.$titelzor.' ';
    }
    else
    {
        echo' '.$value.' != '.$titelzor.' <br /> ';
    }
}

However, it's not working! When a match is supposed to be found, it comes back as (sorry for dutch text):

zovty, bedankt! != zovty, bedankt!

But it's supposed to come back as

zovty, bedankt! === zovty, bedankt!

So the script doesn't seem to detect it as having found a match. What did I do wrong?


Solution

I guess one of the strings may contain trailing whitespace or newline characters, so try this:

if (trim($value) == trim($titelzor)) ...


Answered By - Blagovest Buyukliev
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing