+ 6

[Solved] Use case of "?" and ":" in PHP? (if-else)

Hello my dear friends. Please tell me what is the use case of "?" and ":" in this code: $color = "Green"; $color_default = "Blue"; $var=($color==$color_default)? 1:2; https://code.sololearn.com/wvguk8JOO93C/?ref=app

13th Jul 2022, 10:12 AM
Siavash Kardar Tehran
Siavash Kardar Tehran - avatar
3 Réponses
+ 4
Its a short form of If-else, called ternary operator. If the condition is true then code after ? mark will be executed, else code after : mark will be executed.. edit: Siavash Kardar Tehran ex: $var=(true)? "if true part ":"else part"; echo $var; //output : " if true part" $var=(false)? "true part": "else part"; echo $var; // output : "else part "
13th Jul 2022, 10:18 AM
Jayakrishna 🇮🇳
+ 6
Jayakrishna🇮🇳 Thank you. 🌹❤
13th Jul 2022, 10:33 AM
Siavash Kardar Tehran
Siavash Kardar Tehran - avatar
+ 2
It's also called conditional statements it saves line of code at place of if else condition.
14th Jul 2022, 2:25 AM
Ankit Kumar
Ankit Kumar - avatar