Found 1060 Articles for PHP

PHP variable Variables

Malhar Lathkar
Updated on 19-Sep-2020 15:10:40

2K+ Views

IntroductionIn PHP, it is possible to set variable name dynamically. Such a variable uses value of an existing variable as name. A variable variable is defined with two $ signs as prefixExample Live DemoOutputThis script produces following outputxyz abcd abcd abcdNote that value of $$var1 is same as $xyz, xyz being value of $var1.Numeric value of normal variable cannot be used as variable variableExample Live DemoOutputWhen this script is executed, following result is displayedPHP Parse error: syntax error, unexpected '100' (T_LNUMBER), expecting variable (T_VARIABLE) or '{' or '$' line 6It is also possible to define a variable variable in terms of an ... Read More

PHP Variables from External Sources

Malhar Lathkar
Updated on 19-Sep-2020 15:05:20

341 Views

IntroductionPHP's variable namespace is populated by external sources such as HTML form elements, cookies and screen coordinates of image submit buttonHTML form elementsWhen a web page submits data in its HTML form to a PHP script, it is automatically made available to the script in the form of $_POST, $_GET and $_REQUEST variables. Following is a typical HTML form             Data entered by user is populated as $_POST associative array in PHP scriptPlace HTML page in document root along with testscript.php. Open it in browser and enter dataName : xyz Age : 20Using method='GET' in ... Read More

PHP Variable Basics

Malhar Lathkar
Updated on 19-Sep-2020 15:03:34

387 Views

IntroductionName of a variable in PHP starts with a $ sign. It is followed by either a letter (A-Z either upper or lowe case) or underscore, and then there may be any number of letters, digits or underscores. Name of variable in PHP is case sensitive.Syntax//valid variables $var=10; $VAR="Hello"; //different from $var $marks_1=67; $_val=0; //invalid variables var=10; //not starting with $ $4sqr=16; //not starting with letter/_ $my name="Hello"; //white space not allowed in variable name $my$name="Hello"; //$ character can not be used after first positionA variable is also assigned a value by reference to another variable. To assign value by ... Read More

PHP String Data Type

Malhar Lathkar
Updated on 19-Sep-2020 14:58:10

2K+ Views

Definition and UsageIn PHP, a string data type is a non-numeric sequence of charaters.Any character in the ASCII set can be a part of a string. PHP doesn't support UNICODE.In PHP, literal representation of string can be done with single quotes, double quotes, with heredoc syntax and nowdoc syntax.Syntax//Literal assignment of string value to variable $var='Hello World'; //Single quotes $var3="Hello World"; //Double quotesTo embed single quote character inside single quoted string prefix it with '\'. Similarly to embed backslash in single quoted string prefix it with additional backslash. Other escape sequence characters such as etc. do not carry any ... Read More

PHP Resources

Malhar Lathkar
Updated on 19-Sep-2020 14:53:19

9K+ Views

Definition and UsageIn PHP, Resource  is a special data type that refers to any external resource. A resource variable acts as a reference to external source of data such as stream, file, database etc. PHP uses relevent functions to create these resources. For example, fopen() function opens a disk file and its reference is stored in a resource variable.PHP's Zend engine uses reference conting system. As a result, a resource with zero reference count is destroyed automatically by garbage collector. Hence, memory used by resource data type need not be freed manually.Various types of resources can be handled in a ... Read More

PHP Objects.

Malhar Lathkar
Updated on 04-Oct-2023 12:31:17

32K+ Views

Definition and Usage In PHP, Object is a compound data type (along with arrays). Values of more than one types can be stored together in a single variable. Object is an instance of either a built-in or user defined class. In addition to properties, class defines functionality associated with data. Primary (scalar) variables, arrays and other objects can be cast to object data type using casting operator. PHP provides stdClass as a generic empty class which is useful for adding properties dynamically and casting. Syntax To declare an object of a class we need to use new statement class myclass ... Read More

PHP NULL

Malhar Lathkar
Updated on 19-Sep-2020 14:48:05

7K+ Views

Definition and UsageIn PHP, a variable with no value is said to be of null data type. Such a variable has a value defined as NULL. A variable can be explicitly assigned NULL or its value been set to null by using unset() function.Syntax$var=NULL;It is possible to cast variable of other type to null, although casting null to other type has been deprecated from PHP 7.2. In earlier versions, casting was done using (unset)$var syntaxFollowing example shows how to assign NULL to a variableExample Live DemoOutputThis will produce following result −NULLFollowing example performs null variable to other primary variablesExample Live DemoOutputThis will ... Read More

PHP Iterables

Malhar Lathkar
Updated on 19-Sep-2020 14:41:10

963 Views

Definition and UsageFrom version 7.1 onwards, PHP provides a new pseudo-type called iterable. Any object (such as array) that implements Traversable interface is acceepted by it. This type uses foreach construct or a generator function that yields one value at a a time.SyntaxA function can have iterable as type of its parameter to let the function accept a set of values used in foreach statement. If the parameter doesn't support foreach loop, PHP parser throws TypeErrorExample Live DemoOutputThis will produce following result −PHP Java PythonA PHP function can also return an iterable data type such as array. We use is_iterable() function to ... Read More

PHP Integer Data Type

Malhar Lathkar
Updated on 19-Sep-2020 14:38:43

588 Views

Definition and UsageIn PHP, Integer is a scalar data type that represents a numeric constant represents a whole number wihout any fractional part. PHP allows an integer to be expressed in decimal, hexadecimal, octal or binary number system by prefixing appropriate symbol.By default Integer is assumed in decimal notation. For Hexadecimal, octal and binary number system, respectively 0x, 0 an 0b symbols are prefixed.SyntaxFor better readibility, integer literal may use "_" as separation symbol which will be omitted by PHP scanner while processing.PHP VersionUse of separation symbol "_" is avilable since PHP 7.40Following example shows integer literal representation in different ... Read More

PHP Floating Point Data Type

Malhar Lathkar
Updated on 19-Sep-2020 14:31:53

782 Views

Definition and UsageIn PHP, float data type represents any number with a provision to contain a fractional part. The fractional part may contain digits after decimal point, or may be represented in scientific notation using either e or E. For example 100 in scientific notation is 10e2.Size of a float depends on the hardware/OS platform, although precision upto 14 digits after decimal point is commonly found.Syntax//Literal assignment of float value to variable $var=5327.496; // standard notation $var1=5.327496e3; // Scientific notation $var2=5.327496E3; //Scientific notation $var3=5_327.496; //sepration symbolFor better readibility, integer literal may use "_" as separation symbol which will be omitted by ... Read More

Advertisements