top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Type of a variable in PHP

0 votes
741 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 LinkedIn 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.

0 votes

I have downloaded HipHop for my website, I am going through various web links which are suggesting that performance will improve 3-6 times. However I don't know the Stability of the HipHop.

Please provide your inputs so that I can decide to use HipHop.

0 votes

My webcode written in PHP and it is running in the interpreted way. My problem is it is not giving the desired performance so want to try the compiler if any.
Please suggest if we have any compiler option available for the PHP code and more important is this new option.

...