F
F
Froggyweb2020-05-02 11:18:47
PHP
Froggyweb, 2020-05-02 11:18:47

Assignment to a variable in a ternary operator. Notes?

I assign a variable like this
$demo = ($_POST["demo"]) ? $demo : " ";
. As a result, Notice is given : Undefined index: demo in sendMail.php on line 10

How to do it right, provided that there may not be a demo in the Post?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Evgeny Samsonov, 2020-05-02
@Froggyweb

You should probably assign the value $_POST["demo"] to $demo. Then like this:

$demo = isset($_POST["demo"]) ? $_POST["demo"] : " ";

Or with PHP 7.0 you can use the union operator with null ??
$demo = $_POST["demo"] ?? " ";

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question