Sunday, 8 January 2017

const - How To echo constant Variable in php with variable name




I started learning php and Recently I've faced a problem with a constant variable in my code. recently I've created the Ninja class in the editor and set a stealth constant to the string "MAXIMUM", then I try to echo it to the page using the scope resolution operator (::).






Scope it Out!






class Person {

}
class Ninja extends Person {
// Add your code here...
const stealth = "Maximum";
}
// ...and here!
if(Ninja::stealth){


echo stealth;
}

?>









Now question is "How can echo the const Variable in php???"


Answer



You already accessed it by echo Ninja::stealth;
Try this:
Live Demo : https://eval.in/88040



 class Person {

}
class Ninja extends Person {

// Add your code here...
const stealth = "Maximum";
}
// ...and here!
if(Ninja::stealth){
echo Ninja::stealth;
}


Output:




Maximum

No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...