Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 423221
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T19:09:32+00:00 2026-05-12T19:09:32+00:00

I have an IF statement that consists of two separate function calls passing values

  • 0

I have an IF statement that consists of two separate function calls
passing values to two variables. Obviously if neither value is ‘FALSE’
then the code block is executed:

<?php
   class MyValidater {
      static function validateString($string) {
         if (preg_match("/[A-Za-z]+/", $string)) {
            return $string;
         } else {
            return false;
         }
      }
   }

   $firstName = "Daniel";
   $surname = "Simmons";

   // Dodgy IF statement
   if ($first = MyValidater::validateString($firstName) && $second = MyValidater::validateString($surname)) {
      print("Success: $first $second");
   } else {
      print("Fail: $first $second");
   }
?>

As you can see both the $first and $second variables should contain
the values held in $firstName and $surname after successfully being
validated by the Static method validateString.

However, the values of the two variables end up: $first = ‘1’ and
$second = “Simmons”.

The ‘1’ should be “Daniel” but for some reason $first is being passed
the value ‘1’ or TRUE. If you swap the two assignment statements over
so that $second is evaluated first, you end up with the opposite
outcome as before. $second = ‘1’ and $first = “Daniel”

Can anyone explain why the String value “Daniel” being returned from
the class method is being changed into the int ‘1’ for the first part
of the conditional statement only? I have had a quick look though the
PHP documentation but cannot find an explanation.

For the moment the workaround is to change the return value from the
static method to be true/false and then make $first = $firstName,
etc… upon success. But this involves more code and I would rather
find out why this way does not work.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T19:09:32+00:00Added an answer on May 12, 2026 at 7:09 pm

    You need to bracket your expressions:

    if (($first = MyValidater::validateString($firstName)) && ($second = MyValidater::validateString($surname)))
    

    What’s actually happening is this:

    if ($first = (MyValidater::validateString($firstName) && $second = MyValidater::validateString($surname)))
    

    It would be much clearer to just do this (note this code isn’t identical to what you have):

    $first = MyValidater::validateString($firstName);
    $second = MyValidater::validateString($surname);
    
    if ($first && $second)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 207k
  • Answers 207k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer While I'm at it I may as well write the… May 12, 2026 at 9:21 pm
  • Editorial Team
    Editorial Team added an answer select id from foo inner join bar b1 on b1.fooId=foo.id… May 12, 2026 at 9:21 pm
  • Editorial Team
    Editorial Team added an answer As said in the comments, I think that the problem… May 12, 2026 at 9:21 pm

Related Questions

I have, for my game, a Packet class, which represents network packet and consists
I need to merge two SELECT statements. There are two tables in my database,
In an online manager game (like Hattrick ), I want to simulate matches between
I have a multi-dimensional array, which basically consists of one sub-array for each year.
I am struggling a bit to figure this one out. I'm working on an

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.