Convenience of using php casting to minimize

Is there any difference between php 5. * between

 1.  $variable = (int) 1111;



 2.  $variable = '1111';


in terms of physical resources, for example. memory usage.

Please, any ideas.


source to share

3 answers

If we consider that the next page says:

It should be clear from the above structures that a variable can be of one type, the variable data is represented by the corresponding field in the zval_value union.

This means that the answer to your question is YES . There is a difference in the amount of memory used based on the fact that the variable is indeed represented as internally.

The comments on your question relate to other issues like viability, etc., so I will refrain from that.




A (ASCII) uses 1 byte per character. Int is a fixed length regardless of its value. For "1111" on a 32-bit system, both are 4 bytes. On 64-bit systems, int will take 8 bytes and the string will not change.

Ints can store a value up to 9223372036854775807 on 64-bit systems in 8 bytes. To keep the same number in a line, you must use 19 characters = 19 bytes. Strings can store arbitrarily long numbers, ints have a finite limit.

This ignores any internal zval representation inside PHP that can be added for different types.

However, unless you need to cram millions and millions of numbers into memory all at once (at which point PHP may be the wrong tool for the job you are trying to do), it hardly matters. Use ints for numeric values, use strings for text. If you don't need to store numbers that don't fit into int, use a string. This should be the main problem.



While this is a bit imprecise (it will depend on your system yhour settings), you can try this

$variable = '1111111111';
echo memory_get_usage();


(You will have to reload the page a couple of times until you get a stable result)



All Articles