20

I see myself doing the following code for default assigment all the type on PHP

$variable = $variable ? $variable : 'default value';

I know that with 5.3 I can do

$variable = $variable ?: 'default value';

I would like to further simplify it by being able to just do

$variable ?= 'default value';

and have the variable reassigned in case it evaluates to false. Is it possible to create that assignment? Do I have to compile my own version of php to do that?

hakre
  • 193,403
  • 52
  • 435
  • 836
Yohan Leafheart
  • 860
  • 1
  • 11
  • 27
  • 4
    I'm quite sure you'd have to brew your own version of PHP to do that. – GWW Jun 14 '11 at 17:45
  • Remind, that only you can run code, that relies on such custom features. You usually don't win anything, but a bunch of characters and maybe some annoyed costumers ;) Get in contact to the php-internals mailing list. Maybe its a good thing for the php core. – KingCrunch Jun 14 '11 at 17:46
  • awesome is there somewhere we can open a "petition" to bring it to the attention? – dynamic Jun 14 '11 at 17:50
  • There is the PHP wiki https://wiki.php.net/ . Don't mind the ssl warning (its because they currently use a temporary one). From there have look at the rfc process (and stuff) and (as I mentioned) also have a look at the interals mailing list http://php.net/mailing-lists.php – KingCrunch Jun 14 '11 at 17:54
  • @yohan That operator would be nice to have. – Gavin Jan 24 '15 at 00:18

6 Answers6

24

You cannot create new operators in PHP without changing the PHP source.

And you don't want to change it, trust me. Sure, it'd be easy enough - PHP is open source and uses a straightforward LALR(1) parser which you could easily modify - but that would make your code incompatible with the standard PHP implementation. You would thus very much restrict who can run the code - which will probably be nobody apart from you, as nobody else will care to modify their PHP engine just to run your code.

Update: I wrote a small tutorial on how to add new syntax (like operators) to PHP: https://www.npopov.com/2012/07/27/How-to-add-new-syntactic-features-to-PHP.html

JustCarty
  • 3,839
  • 5
  • 31
  • 51
NikiC
  • 100,734
  • 37
  • 191
  • 225
1

In PHP, you can't create operators or overload the existing operators, such as =.

You can check the package Operator, but, your code will not be runnable withoud it.

KingCrunch
  • 128,817
  • 21
  • 151
  • 173
fvox
  • 1,077
  • 6
  • 8
0

It seems that this is the one example that comes up the most when wondering if it's possible to create new operations. I personally think that ?= would be quite handy.

Since creating new operators would involve creating your own PHP version, and may not very useful for others if you decide to package your code, you can instead easily create a function to emulate the same effect.

/**
 * Global function to simplify the process of
 * checking to see if a value equates to true
 * and setting it to a new value if not.
 * 
 * @param mixed $var the original variable to
 * check and see if it equates to true/false
 * @param mixed $default the default value to
 * replace the variable if it equates to false
 * 
 * @return mixed returns the variable for
 * either further processing or assignment.
 */
function _d( &$var, $default ) {
    return $var = $var ?: $default;
}

You can then call the function, assign it to another variable as required, or even nest them as required to assign default values down a hierarchy of variables.

_d( $variable, 'default value' );
// $variable = $variable ?: 'default value';

$variable1 = _d( $variable, 'default value' ) . 's';
// $variable1 = ( $variable = $variable ?: 'default value' ) . 's';

_d( $variable, _d( $variable1, 'default value' ) );
// $variable = $variable ?: ( $variable1 = $variable1 ?: 'default value' );

EDIT:

As a side note, the function will also assign the default value to the variable even if it has not yet been defined.

You can create a similar function, or modify the function above to the following, if you would prefer to only update the variable if it is not defined.

return $var = isset( $var ) ? $var : $default;}
Shaun Cockerill
  • 800
  • 8
  • 11
0

its simple, php is a php preprocessor, simply preprocess your php file containing your operator to translate all operators you want into it's underlying representation.

otherwise sadly php is not perl/lisp so you can't simply extend the language at runtime to that extent

Dmytro
  • 5,068
  • 4
  • 39
  • 50
0

You cannot do this in PHP,not with this syntax.

<?
$test = array("3 is 5", "3 is not 5"); 
$r = $teste [+( 3 != 5)];
echo $r; //return "3 is not 5" 
?>
The Mask
  • 17,007
  • 37
  • 111
  • 185
-1

You could use a function:

<?php
    function defvar(&$var) {
        if (empty($var)) { $var = 'default value'; }
    }

    // testing
    $set = 'test';
    defvar($set);
    echo $set;

    defvar($notset);
    echo $notset;
?>

Output:

test
default value

Or if you prefer assignment:

<?php
    function setdefvar($var) {
        if (empty($var)) { return 'default value'; }
        else { return $var; }
    }

    // testing
    $set = 'test';
    $set = setdefvar($set);
    echo $set;

    $notset = setdefvar($notset);
    echo $notset;
?>

Output:

test
default value
drudge
  • 35,471
  • 7
  • 34
  • 45