107
<?php
$a = '';

if($a exist 'some text')
    echo 'text';
?>

Suppose I have the code above, how to write the statement if($a exist 'some text')?

desertnaut
  • 57,590
  • 26
  • 140
  • 166
Lucas Fernandes
  • 1,360
  • 2
  • 8
  • 12
  • You mean this: `if($a == 'some text')` . Here some more info over operators: http://www.php.net/manual/en/language.operators.comparison.php – stUrb Mar 08 '13 at 23:49
  • If the size of the string is greater than 0 , then the string has some text in it. – Mohammed Abrar Ahmed Oct 16 '17 at 07:59
  • If you are checking the string if it has any text then this should work `if(strlen($a) > 0) echo 'text';` or if your concern is to check for specific word the follow the @Dai answer. – Mohammed Abrar Ahmed Oct 16 '17 at 08:01

6 Answers6

205

Use the strpos function: http://php.net/manual/en/function.strpos.php

$haystack = "foo bar baz";
$needle   = "bar";

if( strpos( $haystack, $needle ) !== false) {
    echo "\"bar\" exists in the haystack variable";
}

In your case:

if( strpos( $a, 'some text' ) !== false ) echo 'text';

Note that my use of the !== operator (instead of != false or == true or even just if( strpos( ... ) ) {) is because of the "truthy"/"falsy" nature of PHP's handling of the return value of strpos.

As of PHP 8.0.0 you can now use str_contains

<?php
    if (str_contains('abc', '')) {
        echo "Checking the existence of the empty string will always 
        return true";
    }
BugHunterUK
  • 8,346
  • 16
  • 65
  • 121
Dai
  • 141,631
  • 28
  • 261
  • 374
  • Won't a non-match return 0? – Leeish Mar 08 '13 at 23:52
  • 6
    `false >= 0`. You have to write `!== false`, as `0 == false`. – Blender Mar 08 '13 at 23:54
  • @Blender sorry, you're right. I was thinking of the .NET `String.IndexOf` which returns `-1` in event of a non-match. I've corrected my answer. – Dai Mar 09 '13 at 01:54
  • 4
    And `stripos()` for **insensitive** string comparison ... – Pathros Sep 04 '17 at 15:21
  • It's same: if(! strpos( $haystack, $needle ) == false) { echo "\"bar\" exists in the haystack variable"; } more simple and objective... – Padronização S A Jun 27 '18 at 14:16
  • 1
    @PadronizaçãoSA The `!` operator will affect the falsiness of the return value from `strpos` which means `===` won't work the way it's intended. – Dai Jun 27 '18 at 15:04
  • Test it on PHP 7. – Padronização S A Jun 27 '18 at 15:58
  • @Leeish : If the text being searched within the string starts at position 0, its matches and so will return 0. (I know old post but might interest someone reaching this post). – pti_jul Oct 30 '19 at 06:56
  • As docs state: " Return Value: Returns the position of the first occurrence of a string inside another string, or FALSE if the string is not found. Note: String positions start at 0, and not 1." – CodeToLife Oct 31 '19 at 15:22
18

Empty strings are falsey, so you can just write:

if ($a) {
    echo 'text';
}

Although if you're asking if a particular substring exists in that string, you can use strpos() to do that:

if (strpos($a, 'some text') !== false) {
    echo 'text';
}
Blender
  • 289,723
  • 53
  • 439
  • 496
  • 4
    Also if you want it to find "Some Text", "SOME TEXT", etc. use `stripos` (which is case insensitive) – Dave Mar 09 '13 at 00:02
8

http://php.net/manual/en/function.strpos.php I think you are wondiner if 'some text' exists in the string right?

if(strpos( $a , 'some text' ) !== false)
Leeish
  • 5,203
  • 2
  • 17
  • 45
3

If you need to know if a word exists in a string you can use this. As it is not clear from your question if you just want to know if the variable is a string or not. Where 'word' is the word you are searching in the string.

if (strpos($a,'word') !== false) {
echo 'true';
}

or use the is_string method. Whichs returns true or false on the given variable.

<?php
$a = '';
is_string($a);
?>
Jesse
  • 544
  • 5
  • 24
2

You can use strpos() or stripos() to check if the string contain the given needle. It will return the position where it was found, otherwise will return FALSE.

Use the operators === or `!== to differ FALSE from 0 in PHP.

Havenard
  • 27,022
  • 5
  • 36
  • 62
1

You can use the == comparison operator to check if the variable is equal to the text:

if( $a == 'some text') {
    ...

You can also use strpos function to return the first occurrence of a string:

<?php
$mystring = 'abc';
$findme   = 'a';
$pos = strpos($mystring, $findme);

// Note our use of ===.  Simply == would not work as expected
// because the position of 'a' was the 0th (first) character.
if ($pos === false) {
    echo "The string '$findme' was not found in the string '$mystring'";
} else {
    echo "The string '$findme' was found in the string '$mystring'";
    echo " and exists at position $pos";
}

See documentation

Kermit
  • 33,827
  • 13
  • 85
  • 121