What is the difference between declaring an array using "var" and "public" in PHP?
Declaring an array using "var" makes it a private variable within a class, while using "public" makes it a public variable that can be accessed outside the class. If you want to access the array outside the class, you should declare it as a public variable.
class MyClass {
public $arrayPublic = array(); // Declaring as a public variable
}
$obj = new MyClass();
$obj->arrayPublic[] = "element1";
print_r($obj->arrayPublic); // Output: Array ( [0] => element1 )
Keywords
Related Questions
- What is the purpose of the back and next buttons in the while loop?
- How can the dataType attribute in an Ajax request help prevent unexpected HTML tags in the response?
- How can PHP developers optimize sorting functions to prevent timeouts when dealing with large arrays containing special characters?