Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Determining an odd or even string length

Status
Not open for further replies.

jimoblak

Instructor
Oct 23, 2001
3,620
US
Is there a basic function to do determine if a string length is even or odd?

...or do I need to take the string length and divide it by two to see if it is an integer? If so, how would I check to see if it is an integer? ...maybe see if a decimal character exists in the string?

- - picklefish - -
 
String length is always integer - I haven't seen any half characters yet ;).
To find out if it's odd or even use the modulus function.
Code:
$strProperty = strlen($myString)%2 == 0 ? "even" : "odd";

This uses the ternary operator.
 
I meant that the string length divided in half would be a non-integer if it was odd.

I'm trying your suggestion now. Thanks

- - picklefish - -
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top