Skip to content
Advertisement

Tag: syntax

PHP: is there a good syntax to initialize an array element depending on whether it is undefined or not, without using if?

The double question mark syntax in PHP will return null if $bar is undefined. This syntax is extremely useful to simplify code and avoid massive if statements. Now I have the following situation It seems so likely that there exists a one-line simplification for the statement but I just couldn’t come up with it. The closest I come up with

PHP 8 Attributes: Is it possible to validate metadata passed to attributes or force attribute instantiation?

Playing a little bit with the new PHP 8 attributes (annotations) (https://stitcher.io/blog/attributes-in-php-8) I started to create my own ones. Simple example: Hoping that the following code would throw an BadAttributeMetadataException unfortunately it finishes successfully: Is there away to validate the metadata passed for (custom) attributes? Most probably the attributes have to be instantiated automatically somehow. Answer Everything is possible –

PHP weird curly brace syntax?

Does anyone know if PHP has an alternate curly brace syntax? I’ve come across a PHP file with a weird syntax I’ve never seen before. It’s being used in a Wordpress theme. Here’s an example of the …

expecting statement -php syntax | if-else

I get critical error.phpstorms says that expecting statements.I couldnt find a solution Answer You cannot “interrupt” the PHP code after the closing { of the if and before the else keyword. Cleaned up version: To avoid such errors, try learning and following a coding standard, e.g. PSR-12. Since you are working with WordPress, here is WP’s coding standard for PHP:

PHP Difference between array() and []

I’m writing a PHP app and I want to make sure it will work with no errors. The original code: Would the following work with no errors or is not recommended for some reason? Are there any difference? I’ve looked again the data about array() and the short array method with square brackets [] in PHP.net but I’m not sure.

Validate PHP syntax in VIM

I would like to know how if it’s possible to validate if a PHP file is valid in VIM without closing VIM every time? Thank you Answer You can execute shell commands in vim. This is the same as calling php -l filename.php from the shell: I have this mapped into my ~/.vim/after/ftplugin/php.vim file so that I only have to

Advertisement