I want to create a simple command when an array is called. In this case it would be the ON button inside a remote control. (Illustrative concept). But is not working (syntax error).
This is my piece of code:
<?php
class remoteControl{
public $operate = array("ON", "OFF", "UP","DOWN");
public function pressButton($operate("0")){
echo "You have pressed ". $this->operate;
}
}
$control_01 = new remoteControl();
echo $control_01-> pressButton();
?>
Any help would be very greatful 🙂
You have few syntax errors suggesting that you should read PHP manual about basics.
Your code (formatted):
1) you should make this variable private if used only inside class methods
2) using arrays: $operate[0] – read more
3) don’t use string as index (“0”) – it will work, but its unnecessary type casting
4) finally, this line should be something like this:
which means that if you don’t explicitly provide an argument it will have value 0 – read more about function arguments
5) because of 4) it should be:
EDIT: Whole code: