28091

ctype_digit - strange behaviour

Question:

I have a array, and the first 13 values are integer.

Now, if I do :

array_push($pos1, 100);

I'll aspect that value 14 is also an Integer. But in fact, doing :

echo ctype_digit($pos1[12])." - ".ctype_digit($pos1[13]);

The output is 1 -

This is the print_r, as requested :

Array ( [0] => 0 [1] => 1 [2] => 2 [3] => 3 [4] => 4 [5] => 5 [6] => 6 [7] => 7 [8] => 8 [9] => 9 [10] => 10 [11] => 11 [12] => 12 [13] => 100 )

Why?

Answer1:

This is (in fact) a little bit strange, but <a href="http://php.net/ctype-digit" rel="nofollow">ctype_digit()</a> strictly requires a string

echo ctype_digit((string) $pos1[12])." - ".ctype_digit((string) $pos1[13]); // "1 - 1"

I dont know, why PHP doesnt cast it into a string.

However, the 1 in the output comes from the type-cast, because ctype_digit() returns a boolean

echo true; // "1" echo false; // ""

Answer2:

Yes because ctype_digit() function strictly requires a string. You are getting output 1- just because PHP treats its

echo ctype_digit($pos1[12])." - ".ctype_digit($pos1[13]);

as

echo ctype_digit($pos1[12]); //Giving output <em>TRUE</em>

echo " - " ; //Giving output * - * echo ctype_digit($pos1[13]);

<strong>FALSE</strong> because In array the first 13 values are integer and 14th one is not an integer .

Recommend

  • test if a form input is a 1 or 2 digit integer with jquery and php
  • How to sort an array considering localization?
  • Store array in cookie
  • Cookie multiple values
  • Recursive regex not matching template blocks
  • How do I install these PHP extensions on Ubuntu?
  • Installing SSL cert on Amazon EC2 ELB
  • ggplot2 polygon world map centred with limits gives funny edges
  • HTTP Module intercept requests and breaks custom errors configuration
  • Upper limits for fibonnacci
  • Smack 4.1.0 android Roster not displaying
  • Need code translation from VB to C#
  • Upload file that is in the cpan database
  • Z3: Convert between FP and BitVector?
  • Stop Bash Script if Hive Fails
  • How to add git credentials to the build so it would be able to be used within a shell code?
  • Sort List of Strings By Version
  • Date Conversion from yyyy-mm-dd to dd-mm-yyyy
  • Allowing both email and username for authentication
  • How to avoid particles glitching together in an elastic particle collision simulator?
  • Get one-time binding to work for ng-if
  • Counter field in MS Access, how to generate?
  • Deserializing XML into class C#
  • Convert array of 8 bytes to signed long in C++
  • Display Images one by one with next and previous functionality
  • Upload files with Ajax and Jquery
  • How do you troubleshoot character encoding problems?
  • Function pointer “assignment from incompatible pointer type” only when using vararg ellipsis
  • Rearranging Cells in UITableView Bug & Saving Changes
  • Benchmarking RAM performance - UWP and C#
  • Angular 2 constructor injection vs direct access
  • coudnt use logback because of log4j
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • python draw pie shapes with colour filled
  • Django query for large number of relationships
  • costura.fody for a dll that references another dll
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • UserPrincipal.Current returns apppool on IIS
  • Conditional In-Line CSS for IE and Others?