-5

Does PHP have keyword or feature that shortens code and repetition like jquery $(this)?

For example

// How to avoid typing same variable twice
echo isset($_POST['foo']) ? $_POST['foo'] : '';

In Jquery

<script>
    // Avoid typing $("#button-element") again
    $("#button-element").click(function() {
        $(this).css("border-color", "#000");
    });
</script>
Goldbug
  • 517
  • 3
  • 8
  • 1
    But PHP is not built off ECMAScript...? If you want to avoid repetition, use the [null coalesce operator](https://wiki.php.net/rfc/isset_ternary), i.e. `echo $_POST['foo'] ?? '';`, but that's only in PHP 7. – Terry Aug 12 '17 at 22:22
  • 1
    your `this` right now, is treated as a constant, just so you know "this" ;-) You're probably looking for an (PHP) OOP `$this` but it's hard to say really what you're trying to achieve here. – Funk Forty Niner Aug 12 '17 at 22:24
  • Have you tried to research your question before asking it on here? I'm sure something would be on google.... if not then that's also a sign of "no". I don't really understand what you wish to achieve or the real purpose... – NewToJS Aug 12 '17 at 22:24
  • I searched stackoverflow for a similar question, no luck. – Goldbug Aug 12 '17 at 22:25
  • Are you looking for the operator? `?:` – localheinz Aug 12 '17 at 22:25
  • @Fred -ii- Asking to see if there is a shorter way of accessing the same variable, instead of typing the same variable over again. – Goldbug Aug 12 '17 at 22:27
  • 1
    In PHP7 you can `echo $_POST['foo'] ?? 'bar';`. Way cooler. For google: `Null coalescing operator` – jh1711 Aug 12 '17 at 22:29
  • @Goldbug The question is unclear for me, sorry. See if the answer(s) given solves it. – Funk Forty Niner Aug 12 '17 at 22:29
  • `$var = "foo"; echo isset($_POST['foo']) ? $var : '';` maybe? Is that what you're after? – Funk Forty Niner Aug 12 '17 at 22:31
  • https://stackoverflow.com/questions/1523479/what-does-the-variable-this-mean-in-php#1523484 See there! – btc4cash Aug 12 '17 at 22:32
  • You appear to have a significantly distorted understanding of the `this` variable in JavaScript. You should probably read quite a bit on it, at a minimum, [How does the "this" keyword work?](https://stackoverflow.com/q/3127429) – Makyen Aug 13 '17 at 00:33

2 Answers2

2

No. And that code wouldn't work in JS either.

In JS, this is -- roughly speaking -- the object that the current function was called as a method on. It is not "the last thing I mentioned". The JS equivalent of your second code:

_POST['foo'] ? this : ''

would return an object if _POST['foo'] were set. It would not return the value of _POST['foo'].

There is no variable like the one you're looking for in any language I've ever used.

duskwuff -inactive-
  • 171,163
  • 27
  • 219
  • 269
-2

of course it does! you need to construct this across a class. It works about the same as in javascript ES6 class.

$number = 4;
$This = new demoThis($number);
$This->multiplyThis(4);

class demoThis {

private $number;
private $factor;
public $result;

    public function __construct($number) {
        $this->number = $number;
    }

    function multiplyThis($factor) {
        $this->factor = $factor;
        $this->result = $this->number * $this->factor;
        return $this->result;
    }
}

echo isset($_POST['foo']) ? $This->result : '';
cpugourou
  • 755
  • 7
  • 11