Found 1046 Articles for PHP

PHP Increment/Decrement Operators

Malhar Lathkar
Updated on 19-Sep-2020 13:50:56

10K+ Views

IntroductionC style increment and decrement operators represented by ++ and -- respectively are defined in PHP also. As the name suggests, ++ the increment operator increments value of operand variable by 1. The Decrement operator -- decrements the value by 1. Both are unary operators as they need only one operand. These operators (++ or --) can be used in prefix or postfix manner, either as an expression or along with other operators in a more complex expression.Syntax$x=5; $x=5; $y=5; $x++; //postfix increment $y--; //postfix decrement ++$y; //prefix increment --$x; //prefix decrementWhen used independently, postfix and prefix increment/decrement operator ... Read More

PHP Execution Operator

Malhar Lathkar
Updated on 19-Sep-2020 13:47:10

1K+ Views

IntroductionThere is one Execution operator defined in PHP. A string inside back-ticks (``) is treated as a DOS command (a shell command in UNIX/Linux) and its output is returned. This operator is similar in operation to shell_exec() function in PHP.Following code executes DIR command and returns result as string.ExampleOutputFollowing result will be displayedVolume in drive C is Windows 10 Volume Serial Number is 540D-CE99 Directory of C:\xampp\php 01/27/2016 05:32 PM 18, 869 CompatInfo.php 07/08/2020 06:40 PM 64 test.php 07/11/2020 02:13 PM 48 testscript.php 03/30/2013 05:59 PM 1, 447 webdriver-test-example.php 4 File(s) 20, 428 bytes 0 Dir(s) 178, 002, 157, 568 ... Read More

PHP Error Control Operator

Malhar Lathkar
Updated on 19-Sep-2020 13:42:18

1K+ Views

IntroductionIn PHP @ symbol is defined as Error Control Operator. When it is prefixed to any expression, any error encountered by PHP parser while executing it will be suppressed and the expression will be ignored.Following code tries to open a non-existing file for read operation, but PHP parser reports warningExample Live DemoOutputFollowing result will be displayedHello World PHP Warning: fopen(nosuchfile.txt): failed to open stream: No such file or directory in /home/cg/root/1569997/main.php on line 2Prepending @ symbol to fopen() expression suppresses error message and statement itself is ignoredExample Live DemoOutputFollowing result will be displayedHello World

PHP Expressions

Malhar Lathkar
Updated on 19-Sep-2020 13:18:02

7K+ Views

IntroductionAlmost everything in a PHP script is an expression. Anything that has a value is an expression. In a typical assignment statement ($x=100), a literal value, a function or operands processed by operators is an expression, anything that appears to the right of assignment operator (=)Syntax$x=100; //100 is an expression $a=$b+$c; //b+$c is an expression $c=add($a, $b); //add($a, $b) is an expresson $val=sqrt(100); //sqrt(100) is an expression $var=$x!=$y; //$x!=$y is an expressionexpression with ++ and -- operatorsThese operators are called increment and decrement operators respectively. They are unary operators, needing just one operand and can be used in prefix or ... Read More

PHP goto Statement

Malhar Lathkar
Updated on 19-Sep-2020 13:14:00

4K+ Views

IntroductionThe goto statement is used to send flow of the program to a certain location in the code. The location is specified by a user defined label. Generally, goto statement comes in the script as a part of conditional expression such as if, else or case (in switch construct)Syntaxstatement1; statement2; if (expression)    goto label1; statement3; label1: statement4;After statement2, if expression (as a part of if statement) is true, program flow is directed to label1. If it is not true, statement3 will get executed. Program continues in normal flow afterwards.In following example, If number input by user is even, program ... Read More

PHP if else elseif

Malhar Lathkar
Updated on 19-Sep-2020 13:07:18

2K+ Views

IntroductionConditional execution of one or more statements is a most important feature of any programming language. PHP provides this ability with its if, else and elseif statements. Primary usage of if statement is as follows −Syntaxif (expression)    statement;The expression in front of if keyword is a logical expression, evaluated either to TRUE or FALSE. If its value is TRUE, statement in next line is executed, otherwise it is ignored. If there are more than one statements to be executed when the expression is TRUE, statements are grouped by using additional pair of curly brackets, if (expression){    statement1;   ... Read More

PHP Constants

Malhar Lathkar
Updated on 19-Sep-2020 15:00:15

257 Views

IntroductionConstants are represented literally in an assignment expression such as $x=10 or $name="XYZ" where 10 and XYZ are numeric and string constants assigned to variables. In PHP, it is possible to define a constant with a user defined identifier with the help of define() functionSyntaxdefine ( string $name , mixed $value [, bool $case_insensitive = FALSE ] ) : boolparametersSr.NoParameter & Description1namename of the constant.2valuevalue of the constant may be any scalar value (integer, float, string etc) or array3case_insensitiveConstant identifiers are case sensitive by default. If this parameter is set to true, name and NAME are treated similarlyReturn ValueFunction returns ... Read More

PHP Variable functions

Malhar Lathkar
Updated on 18-Sep-2020 14:00:58

8K+ Views

IntroductionIf name of a variable has parentheses (with or without parameters in it) in front of it, PHP parser tries to find a function whose name corresponds to value of the variable and executes it. Such a function is called variable function. This feature is useful in implementing callbacks, function tables etc.Variable functions can not be built eith language constructs such as include, require, echo etc. One can find a workaround though, using function wrappers.Variable function exampleIn following example, value of a variable matches with function of name. The function is thus called by putting parentheses in front of variableExample Live ... Read More

PHP User-defined functions

Malhar Lathkar
Updated on 18-Sep-2020 13:57:22

17K+ Views

IntroductionPHP has a large number of built-in functions such as mathematical, string, date, array functions etc. It is also possible to define a function as per specific requirement. Such function is called user defined function.A function is a reusable block of statements that performs a specific task. This block is defined with function keyword and is given a name that starts with an alphabet or underscore. This function may be called from anywhere within the program any number of times.Syntax//define a function function myfunction($arg1, $arg2, ... $argn) {    statement1;    statement2;    ..    ..    return $val; } ... Read More

PHP Function arguments

Malhar Lathkar
Updated on 18-Sep-2020 13:38:56

4K+ Views

IntroductionA function in PHP can be defined to accept input from calling environment/script in the form of arguments. These arguments are given as comma separeted list inside the parentheses in front of name of function. Note that while calling a function, same number of arguments must be passed to it.PHP supports calling a function by passing value, reference, arguments with default value and by passing variable number of arguments.function with argumentsIn following example, a function is defined with two formal arguments. When this function is called by passing the arguments by value. Arguments of function become its local variables. Hence, ... Read More

Advertisements