variable-types

Declaring Variable Types in PHP?

折月煮酒 提交于 2019-12-20 10:16:06
问题 I was trying to get my Netbeans to autocomplete with PHP, and I learned that this code is valid in PHP: function blah(Bur $bur) {} A couple of questions: Does this actually impose any limits on what type of variable I can pass to the blah method? If this is just to help the IDE, that's fine with me. How can I declare the type of a variable in PHP if I'm not in a function? 回答1: This type-hinting only works for validating function arguments; you can't declare that a PHP variable must always be

Check if a macro argument is a pointer or not

邮差的信 提交于 2019-12-17 16:58:14
问题 Is there some "nice" way to check if a variable passed to a macro is a pointer? e.g. #define IS_PTR(x) something int a; #if IS_PTR(a) printf("a pointer we have\n"); #else printf("not a pointer we have\n"); #endif The idea is that this is not done run-time but compile-time, as in: we get different code depending on if the variable is a pointer or not. So I would like IS_PTR() to evaluate to some kind of constant expression in some way. Am I going about this idea all the wrong way? 回答1: It is

How to check if a variable is an array?

巧了我就是萌 提交于 2019-12-10 17:10:17
问题 I was playing with PROCINFO and its sorted_in index to be able to control the array transversal. Then I wondered what are the contents of PROCINFO , so I decided to go through it and print its values: $ awk 'BEGIN {for (i in PROCINFO) print i, PROCINFO[i]}' ppid 7571 pgrpid 14581 api_major 1 api_minor 1 group1 545 gid 545 group2 1000 egid 545 group3 10004 awk: cmd. line:1: fatal: attempt to use array `PROCINFO["identifiers"]' in a scalar context As you see, it breaks because there is -at

Declaring Variable Types in PHP?

无人久伴 提交于 2019-12-02 21:37:12
I was trying to get my Netbeans to autocomplete with PHP , and I learned that this code is valid in PHP: function blah(Bur $bur) {} A couple of questions: Does this actually impose any limits on what type of variable I can pass to the blah method? If this is just to help the IDE, that's fine with me. How can I declare the type of a variable in PHP if I'm not in a function? JW. This type-hinting only works for validating function arguments; you can't declare that a PHP variable must always be of a certain type. This means that in your example, $bur must be of type Bur when "blah" is called, but

Type hinting for properties in PHP 7?

梦想与她 提交于 2019-11-30 22:18:00
问题 Does php 7 support type hinting for class properties? I mean, not just for setters/getters but for the property itself. Something like: class Foo { /** * * @var Bar */ public $bar : Bar; } $fooInstance = new Foo(); $fooInstance->bar = new NotBar(); //Error 回答1: PHP 7.4 will support typed properties like so: class Person { public string $name; public DateTimeImmutable $dateOfBirth; } PHP 7.3 and earlier do not support this, but there are some alternatives. You can make a private property which

How can I check if a var is a string in JavaScript?

狂风中的少年 提交于 2019-11-28 16:46:01
How can I check if a var is a string in JavaScript? I've tried this and it doesn't work... var a_string = "Hello, I'm a string."; if (a_string typeof 'string') { // this is a string } You were close: if (typeof a_string === 'string') { // this is a string } On a related note: the above check won't work if a string is created with new String('hello') as the type will be Object instead. There are complicated solutions to work around this, but it's better to just avoid creating strings that way, ever. alex The typeof operator isn't an infix (so the LHS of your example doesn't make sense). You

Check if a macro argument is a pointer or not

一个人想着一个人 提交于 2019-11-28 01:51:32
Is there some "nice" way to check if a variable passed to a macro is a pointer? e.g. #define IS_PTR(x) something int a; #if IS_PTR(a) printf("a pointer we have\n"); #else printf("not a pointer we have\n"); #endif The idea is that this is not done run-time but compile-time, as in: we get different code depending on if the variable is a pointer or not. So I would like IS_PTR() to evaluate to some kind of constant expression in some way. Am I going about this idea all the wrong way? It is certainly not observable through the preprocessor in #if as you imply in your question. The preprocessor

How can I check if a var is a string in JavaScript?

谁说胖子不能爱 提交于 2019-11-27 09:55:05
问题 How can I check if a var is a string in JavaScript? I've tried this and it doesn't work... var a_string = "Hello, I'm a string."; if (a_string typeof 'string') { // this is a string } 回答1: You were close: if (typeof a_string === 'string') { // this is a string } On a related note: the above check won't work if a string is created with new String('hello') as the type will be Object instead. There are complicated solutions to work around this, but it's better to just avoid creating strings that

Enabling 'strict_types' globally in PHP 7

六月ゝ 毕业季﹏ 提交于 2019-11-27 09:23:51
I'm currently migrating my website from PHP5 to PHP7, and I've started using the strict typing feature that was added. However this requires me to begin all files with the following line: <?php declare(strict_types=1); // All other code here // ... So I was wondering, is there any way to enable strict_types globally using something like php.ini or the apache configuration file so I don't have to write this line every time, and if so how could I enable this? This is deliberately not possible, because the implementation adopted after an extremely long discussion of scalar type hints was this one

Enabling &#39;strict_types&#39; globally in PHP 7

ぐ巨炮叔叔 提交于 2019-11-26 14:40:24
问题 I'm currently migrating my website from PHP5 to PHP7, and I've started using the strict typing feature that was added. However this requires me to begin all files with the following line: <?php declare(strict_types=1); // All other code here // ... So I was wondering, is there any way to enable strict_types globally using something like php.ini or the apache configuration file so I don't have to write this line every time, and if so how could I enable this? 回答1: This is deliberately not