PHP | var keyword Last Updated : 26 May, 2021 Summarize Comments Improve Suggest changes Share Like Article Like Report The var keyword in PHP is used to declare a property or variable of class which is public by default. The var keyword is same as public when declaring variables or property of a class.Note: The var keyword was deprecated from version 5.0.0 up to version 5.1.2. Since PHP 5.1.3 it has been added again.Syntax: class className { var $variable = "GeeksforGeeks"; // Other statements } Below programs illustrate the var keyword in PHP:Program 1: This program illustrates the var keyword. php <?php // Declaring a class class Geeks { // Using var keyword // to declare Public variable var $var1 = 'Public'; // Declaring protected variable protected $var2 = 'Protected'; // Declaring private variable private $var3 = 'Private'; } // Creating an object $obj = new Geeks(); // Calling var declared variable echo $obj->var1 . "\n";; ?> Output: Public Program 2: This program illustrates the var and public keyword. php <?php // Declaring a class class Geeks { // Using var keyword // to declare Public variable var $var1 = 'Var Public'; // Using public keyword // to declare Public variable public $var2 = 'Public'; } // Creating an object $obj = new Geeks(); // Calling var declared variable echo $obj->var1 . "\n"; // Calling public declared variable echo $obj->var2 . "\n";; ?> Output: Var Public Public Program 3: This program demonstrating the error while calling private variables. php <?php // Declaring a class class Geeks{ // Using var keyword // to declare Public variable var $var1 = 'Var Public'; // Using private keyword // to declare private variable private $var2 = 'Private'; } // Creating an object $obj = new Geeks(); // Calling var declared variable echo $obj->var1 . "\n";; // Calling private declared variable // It will give error echo $obj->var2 . "\n";; ?> Output: Var Public Error: PHP Fatal error: Uncaught Error: Cannot access private property Geeks::$var2 in /home/46488c166fd1197d687867f62e03b8b8.php:24 Stack trace: #0 {main} thrown in /home/46488c166fd1197d687867f62e03b8b8.php on line 24 Comment More infoAdvertise with us Next Article PHP | var keyword R Rajnis09 Follow Improve Article Tags : Web Technologies PHP PHP Programs PHP-keyword Similar Reads PHP Full Form The Full form of PHP is Hypertext Preprocessor and earlier it was abbreviated as Personal Home Page. It is a general-purpose programming language used to design a website or web application. It is the server-side scripting language embedded with HTML to develop Static website, Dynamic website, or We 3 min read PHP Array Programs PHP Arrays is a type of data structure that allows us to store multiple elements of similar data type under a single variable thereby saving us the effort of creating a different variable for every data. S. No Articles 1PHP Program to Insert a New Element in an Array2PHP Program to Find the Index of 4 min read PHP 7 | Features Lots of features have been added in PHP7. After the release of PHP7, performance has been increased by 25-70% for the website. Now we will discuss the PHP7 features one by one through example. Scalar Datatype Hinting: Scalar datatypes are boolean, integer, float and string. Earlier we were unable to 11 min read How to Copy String in PHP ? Copying a string is a basic operation that all the programming languages offer. In this article, we will explore different approaches to copying a string in PHP, including using the assignment operator, the strval() function, the substr() function and by implementing the strcpy() function.Table of C 3 min read PHP foreach Loop The foreach loop in PHP is a powerful and convenient way to iterate over arrays and objects. The foreach loop though iterates over an array of elements, the execution is simplified and finishes the loop in less time comparatively. In this article, we will explore the foreach loop in detail, includin 3 min read Like