0

Stupid question time.

This if statement always shows nothing.

$new = 'test = new user id';

if (stripos($new, 'test') !== FALSE) {
    $chk = explode("=",$new);
    $chk = $chk[1];
    echo $chk;
} else {
    $chk = '';
}

echo $chk;

When I echo $chk within the if else it shows the correct value, but the final echo is always blank.

Why?

Shouldn't $chk = "new user id" if the string contains that?

Also is there a neater more succinct way of writing this?

Thanks

3
  • 1
    Seems to perform fine for me: 3v4l.org/blkhW Commented Mar 28, 2014 at 10:26
  • For me its getting fine Commented Mar 28, 2014 at 10:26
  • Could you provide the whole code? Commented Mar 28, 2014 at 10:29

3 Answers 3

1

use this code. it works fine.

$new = 'test = new user id';

if (stripos($new, 'test') === FALSE) {
        $chk = '';

    } else {
          $chk = explode("=",$new);
        $chk = $chk[1];


    }

    echo $chk;
Sign up to request clarification or add additional context in comments.

Comments

0

Initialize $chk before your condition statement.

$new = 'test = new user id';
$chk = '';
if (stripos($new, 'test') !== FALSE) {
        $chk = explode("=",$new);
        $chk = $chk[1];
        echo $chk;
    } else {
        $chk = '';
    }

    echo $chk;

3 Comments

And how this relate's to the question asked by OP's ?
Why do you think this answers the question at all? What does it fix? It initializes a variable, which isn't necessary at all.
That is always a good idea, but it won't help here simply because he is changing $chk in both blocks...
0

try this:

<?php
    $new = 'test = new user id';
    $start = strpos($new,'=')?strpos($new,'='):strlen($new);
    echo substr($new,$start +1);
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.