top button
Flag Notify
    Connect to us
      Facebook Login
      Site Registration Why to Join

Facebook Login
Site Registration
Print Preview

Type of a variable in PHP

0 votes
53 views

Have two questions -
1. How to find type of a variable in PHP.
2. How to find the type of an array in PHP.

Please help.

posted Mar 15, 2013 by Salil

Share this question
Facebook Share Button Twitter Share Button Google+ Share Button LinkedIn Share Button Multiple Social Share Button

3 Answers

+1 vote
 
Best answer

Use this:

http://php.net/manual/en/function.gettype.php 
answer Mar 15, 2013 by anonymous
The nice thing about PHP 'arrays' is that they are a not restricted to a single type. The elements of an array can be different types. It's more a 'basket' than a traditional array.
0 votes

How to find type of a variable in PHP.

gettype(), or one of the is_*()-functions. But for me more 
interesting for me: Why do you _need_ this?

How to find the type of an array in PHP.

An array is of type array :) is_array() or again gettype().

Can you maybe describe what you want to achieve? In a dynamically typed language it is rarely _required_, that you know the _concrete_ type.

answer Mar 16, 2013 by Kevin Peterson
0 votes
 function get_type($var) 
 { 
    if(is_object($var)) 
         return get_class($var); 
     if(is_null($var)) 
         return null; 
     if(is_string($var)) 
         return string; 
     if(is_array($var)) 
         return array; 
     if(is_int($var)) 
         return integer; 
     if(is_bool($var)) 
         return boolean; 
     if(is_float($var)) 
         return float; 
     if(is_resource($var)) 
         return resource; 
     return unknown; 
 } 
answer Mar 16, 2013 by anonymous
Similar Questions
0 votes

I am having an string which was have few ' (single quote) and few " (double quotes) and was not able to insert into the mysql database. I have replaced them with \' and \" and everything is fine.
Though this are fine now but don't understand the working and I could have missed few corner cases also. Please suggest the working and also if there is some better way to achieve this.

+1 vote

I have a web application written in PHP. It have been running for several years. Now I want to run it as a stand-alone application on an Android smartphone or tablet. How can I do it?

+1 vote

In my database design, I tend to store some variable that is meant to be acting as a ROLE or TYPE as SMALLINT. For example :

CREATE TABLE `house` (
   `id` int(11) NOT NULL AUTO_INCREMENT,
   `type` smallint(11) NOT NULL,
)

And in php, I do

define('HOUSE_SMALL_TYPE', '0');
define('HOUSE_MEDIUM_TYPE', '1');

So in php, in SELECT queries I do :

$this->db->query("SELECT * FROM house  
                    WHERE type=?;", HOUSE_SMALL_TYPE);

My questions are :
1. In the php part, is there is a better way to do this ?
2. In the mysql itself, does mysql also has global define functionality (like the define in php) ? I also want to do kind of SELECT * FROM house WHERE type = HOUSE_SMALL_TYPE in mysql query.

+1 vote

I have a code in PHP something like this

 if (strlen($profile['current-employer']) == 0)
     break;
 else
     return a;

Now in some cases $profile['current-employer'] can be non-existent, now here my question is in the case of non-existent variable what strlen will result... (This is related with QueryHome code)


Useful Links with Similar Problem
Contact Us
+91 9880187415
sales@queryhome.net
support@queryhome.net
#470/147, 3rd Floor, 5th Main,
HSR Layout Sector 7,
Bangalore - 560102,
Karnataka INDIA.
QUERY HOME
...