PHP

Topic: String

What Is the Best Way to Test the strpos() Return Value?

Because strpos() could two types of values, Integer and Boolean, you need to be careful about testing the return value. The best way is to use the "Identical(===)" operator. Do not use the "Equal(==)" operator, because it does not differentiate "0" and "false". Check out this PHP script on how to use strpos():<?php$haystack = "needle234953413434516504381640386488129";$pos = strpos($haystack, "needle");if ($pos==false) {  print("Not found based (==) test\n");} else {  print("Found based (==) test\n");}if ($pos===false) {  print("Not found based (===) test\n");} else {  print("Found based (===) test\n");}?>This script will print:Not found based (==) testFound based (===) testOf course, (===) test is correct.

Browse random answers:

How To Count the Number of Characters in a String?
How Many ways to Remove Leading and Trailing White Spaces?
How To Remove Leading and Trailing Spaces from User Input Values?
How to Find a Substring from a Given String?
What Is the Best Way to Test the strpos() Return Value?
How To Take a Substring from a Given String?
How To Replace a Substring in a Given String?
How To Reformat a Paragraph of Text?
How To Convert Strings to Upper or Lower Cases?
How To Convert Leading Characters to Upper Case?
How To Compare Two Strings with strcmp()?
How To Convert Strings to Hex Numbers?
How To Generate a Character from an ASCII Value?
How To Convert a Character to an ASCII Value?
How To Split a String into Pieces?
How To Join Multiple Strings into a Single String?
How To Replace a Group of Characters by Another Group?
How Many Escape Sequences Are Recognized in Single-Quoted Strings?
Can You Specify the "new line" Character in Single-Quoted Strings?
What Are the Special Characters You Need to Escape in Double-Quoted Stings?
How To Include Variables in Double-Quoted Strings?
How Many Ways to Include Array Elements in Double-Quoted Strings?
How To Access a Specific Character in a String?
How To Assigning a New Character in a String?
How To Concatenate Two Strings Together?
How To Compare Two Strings with Comparison Operators?
How To Convert Strings to Numbers?
How To Count the Number of Characters in a String?
How To Remove the New Line Character from the End of a Text Line?
How To Remove Leading and Trailing Spaces from User Input Values?
How to Find a Substring from a Given String?
How To Remove the New Line Character from the End of a Text Line?
How to Find a Substring from a Given String?
How To Take a Substring from a Given String?
How To Replace a Substring in a Given String?
How To Reformat a Paragraph of Text?