Showing posts with label PHP gettype. Show all posts
Showing posts with label PHP gettype. Show all posts

Friday, 2 August 2019

How to detect the data type of a PHP variable

The data type of a PHP variable can be detected by using the built-in PHP functiongettype() (PHP 4, PHP 5)
Example:
$var ="This is a sentence"

echo $var; //This will return "string"
The following possible values can be returned:
  • boolean
  • integer
  • double
  • string
  • array
  • object
  • resource
  • NULL
  • unknown type

Monday, 2 February 2015

Format Debugging Messages

Format Debugging Messages

<?php
function debug( $line, $msg ){
    static 
$calls = 1;
    print 
"<P><HR><br>\n";
    print 
"DEBUG $calls: Line $line: $msg<br>";
    
$args = func_get_args();
    
    if (  
count( $args ) % 2 )
        print 
"Odd number of args<BR>";
    else{
        for ( 
$x=2; $x< count($args); $x += 2 ){
            print 
"&nbsp&nbsp; \$$args[$x]: ".$args[$x+1];
            print 
" .... (".gettype( $args[$x+1] ).")<BR>\n";
        }
    }
    print 
"<hr><p></p>\n";
    
$calls++;
}
$test = 55;debug( __LINE__, "First message", "test", $test );$test = 66;$test2 = $test/2;debug( __LINE__, "Second message", "test", $test, "test2", $test2 );?>

Saturday, 4 October 2014

popen in PHP

popen — Opens process file pointer
Syntax:

resource popen ( string $command , string $mode )
Opens a pipe to a process executed by forking the command given by command.

Parameters:

command
The command

mode
The mode

Return values: Returns a file pointer identical to that returned by fopen(), except that it is unidirectional (may only be used for reading or writing) and must be closed with pclose(). This pointer may be used with fgets(), fgetss(), and fwrite(). When the mode is 'r', the returned file pointer equals to the STDOUT of the command, when the mode is 'w', the returned file pointer equals to the STDIN of the command.

If an error occurs, returns FALSE.



Example #1 popen() example

<?php
$handle = popen("/bin/ls", "r");
?>
If the command to be executed could not be found, a valid resource is returned. This may seem odd, but makes sense; it allows you to access any error message returned by the shell:

Example #2 popen() example

<?php
error_reporting(E_ALL);

/* Add redirection so we can get stderr. */
$handle = popen('/path/to/executable 2>&1', 'r');
echo "'$handle'; " . gettype($handle) . "\n";
$read = fread($handle, 2096);
echo $read;
pclose($handle);
?>


Note:
If you're looking for bi-directional support (two-way), use proc_open().
Note: When safe mode is enabled, you can only execute files within the safe_mode_exec_dir. For practical reasons, it is currently not allowed to have .. components in the path to the executable.
Warning
With safe mode enabled, the command string is escaped with escapeshellcmd(). Thus, echo y | echo x becomes echo y \| echo x.

Friday, 3 October 2014

gettype in PHP

gettype — Get the type of a variable
Syntax:string gettype ( mixed $var )
Returns the type of the PHP variable var. 
        For type checking, use is_* functions.
Parameter:var
    The variable being type checked.
Return Values:
Possible values for the returned string are:
        "boolean"
        "integer"
        "double" (for historical reasons "double" is returned in case of a float, and not simply "float")
        "string"
        "array"
        "object"
        "resource"
        "NULL"
        "unknown type"

Example #1
<?php
$data = array(1, 1., NULL, new stdClass, 'foo');
foreach ($data as $value) {
    echo gettype($value), "\n";
}
?>
O/P:
integer
double
NULL
object
string