• Unfortunately, we have experienced significant hard drive damage that requires urgent maintenance and rebuilding. The forum will be a state of read only until we install our new drives and rebuild all the configurations needed. Please follow our Facebook page for updates, we will be back up shortly! (The forum could go offline at any given time due to the nature of the failed drives whilst awaiting the upgrades.) When you see an Incapsula error, you know we are in the process of migration.

[PHP] CType functions

Experienced Elementalist
Joined
Jul 23, 2012
Messages
201
Reaction score
128
Here's basic introduction to php ctype functions, these function are used to validate string values if its correct type(alpha numeric, integer,string,etc).

Warning: You should always cast integer variables to string, because values between -125 and 256 are converted to their ASCII equivalent values.
Example:
PHP:
<?php
$integer = 38;

ctype_digit($integer);        // false, because ASCII 38 is '&'
is_numeric($integer);         // true

Available functions


  • ctype_alnum() : check for alphanumeric characters (A : Z, upper or lower case, 0-9, no special characters, punctuation, or other freaks).
  • ctype_alpha() : check for alphabetical characters (A-Z, upper or lower case).
  • ctype_cntrl() : check for control characters (things like \n, etc.).
  • ctype_digit() : check for numeric characters (0-9, no decimal point, comma, etc.).
  • ctype_graph() : check for visually printable characters (not control characters or space).
  • ctype_lower() : check for lowercase characters (a-z lower case only, no numbers).
  • ctype_print() : check for printable characters including control characters and space.
  • ctype_punct() : check for punctuation type characters (no numbers, letters, or space). These include “special” characters like @&!#
  • ctype_space() : check for white space characters (includes blanks and any control character that leaves a space on the page.
  • ctype_upper() : check for uppercase characters (A-Z upper case only, no numbers or special characters).
  • ctype_xdigit() : check for hex characters.
Usage Example
PHP:
<?php

if(ctype_lower('ABab10'))
{
 echo 'all is okey!';
}
else
{
 echo 'input is wrong';
}

Result for this example would be: input is wrong
becouse ctype_lower only can contain only: a-z lower case only, no numbers

There are other functions to validate string like is_*(is_string,is_float,is_integer,is_numeric)
 
Watching from above
Legend
Joined
Apr 9, 2004
Messages
3,828
Reaction score
752
You know, to make it a more useful tutorial/introduction you should begin with what the problem is that your subject solves. You could show some common mistakes and errors when using the typical patterns and then contrast them with how to solve the problems properly with the approach that you're telling us about.

You answered a What with your post.. to make it complete you should answer Why - What - How - When (& When not)

I don't mean this as an offense by the way. I checked your post with curiosity, but I couldn't grasp why or who should read this within the first 5 seconds so you lost my attention span there and I'm afraid the same happens with most people - even those who might benefit from it. :rolleyes:
 
Back
Top