Hey guys, I have a quick question. I normally only do Front-End but I have just started working more with PHP. So I have a question regarding conditionals. Here is my code:

//I actually get this from when a user posts, this is for demo
$videoURL = "http://www.youtube.com/watch?v=-XjwL9kCmgc";

$parsedURL = parse_url($videoURL);
$values=array_values($parsedURL);
$videoProvider = $values[1]; //prints first value aka host (www.youtube.com)

if($videoProvider != 'www.youtube.com' || $videoProvider != 'vimeo.com') {
    $errors[] = "<p>$videoProvider is not allowed. Must be from Youtube or Vimeo!</p>";
}
else if () {

}
else {
 //do this... submit form
}

So what's happening is the initial if statment with the || conditional is not excuting it is allowing all videos to submitted still ... I have other conditonals below it that work fine like

   `else if (strlen($videoURL)<0){
        $errors[] = "<p>Video URL empty!</p>";
    }`

Any help would be greatly appreciated! Thanks :)

Recommended Answers

All 4 Replies

I suggest you use extra parenthesis to force correct execution:

if (($videoProvider != 'www.youtube.com') || ($videoProvider != 'vimeo.com'))

The extra parenthesis work when I remove || ($videoProvider != 'vimeo.com') but for some reason that OR statment breaks it ... not sure why? I removed it and submitted youtube url and it worked fine like this .. not sure what I'm doing wrong. Thanks for the help btw, any suggestions?

if (($videoProvider != 'www.youtube.com')){

Probably your logic. I think what you want to happen is (I didn't check the first time):

if (($videoProvider != 'www.youtube.com') && ($videoProvider != 'vimeo.com'))

I replaced or with and, because you want to add the error when your video provider is other than those two. Another way to write the above is:

if (! (($videoProvider == 'www.youtube.com') || ($videoProvider == 'vimeo.com')))

Ahh I see! You're awesome it worked, I need to sleep lol! Thanks man :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.