Creating a set of variables in PHP

Asked

Viewed 149 times

2

I am new to PHP, and I have some doubts. I noticed that when variables are created, types are not specified.

So:

  • how do I create the variables of type short int, int and char? It creates the type of variable on startup of the same?

  • and how I could check the size of the variable in bytes?

  • In addition, it is possible to create a set of variables in PHP, and from an identifier of this set, assign values to these variables?

  • Have a look at http://php.net/manualen/language.oop5.typehinting.php

  • I don’t understand ....

  • 1

    I think that "Vista de olhos" in Portuguese is to "take a look" at Nrasil

  • I did not understand the content of the last link ... I could not relate to my question

1 answer

3


PHP is a weak typing language. This means that it is not necessary to define a type for the variable.

So it’s like you said:

it creates the type of variable at startup of the same

Example:

 $int = 1;

 $float = 1.44;

 $string = 'stackoverflow';

 $boolean = false;

 $array = array();

 $object = new stdClass;

You can test the types of these variables through functions such as is_string, is_float, is_int and is_boolean.

You can also get the variable type name through the function gettype.

Example:

$array = array(1, 2, 3);

gettype($array); // array

Induction of Types

In PHP it is possible to "induce" specific (not all) types for parameters passed in functions.

We can check if a given object is of a certain instance or subinstication. If it implements an interface, if it is a callback, or if it is an array.

Examples of type induction for classes:

class X{
   public function get(){}
}

function usa_x(X $x){
    return $x->get();
}

usa_x(new X);

Examples of type induction for arrays:

function usa_array(array $array){
  foreach($array as $key => $value);
}

usa_array(1); // gera um erro!

Examples of type induction for callbacks (from php 5.4):

function usa_callback(callable $callback)
{
     return $callback(1, 2, 3);
}
usa_callback('var_dump');

usa_callback(function ($a, $b, $c){
   var_dump($a, $b, $c);
});

usa_callback('nao_existe'); // gera um erro

Spltype

There’s an extension called Spl Type, where it is possible to simulate the definition of types through instances of specific classes for each type.

Example:

$int = new SplInt(94);

try {
    $int = 'Try to cast a string value for fun';
} catch (UnexpectedValueException $uve) {
    echo $uve->getMessage() . PHP_EOL;
}

echo $int . PHP_EOL; // Value is not integer

If this is necessary (I believe in most cases it is not), here is the link to Spltype

  • Interesting thing to do... Another thing, this array in PHP, is similar to the struct of C? why did I search, in the array, we can create variables that are called "keys" and assign values to them from the instance of the array, right?

  • Yes. In PHP the array (currently - as the documentation says) is an ordered matrix. It’s not just a list where you start from 0 and go to where you stop adding elements to that list!

  • What I intend to do is to send this array via socket to a program in C, and this C program must move the memory block of that array up to a struct that contains the same data set. Would it be possible? If not, because?

  • @Vynstus, in this case already changes the focus of the question. I suggest that you create a new question or modify the current one.

  • I created, http://answall.com/questions/76845/criando-buffers-em-php

Browser other questions tagged

You are not signed in. Login or sign up in order to post.