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
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
$Errorview variable determines the display format of the error message in PowerShell. Before PowerShell 7 there were mainly two views, Normal View (Default view)Category ViewWith PowerShell version 7, one new additional error view category is included and now there are 3 $ErrorView categories for version 7.Concise View (Default)Normal ViewCategory ViewWe will understand each view one by one.A ) Normal ViewIt is the default view before PowerShell version 7 and it produces the detailed multiline errors and bit noisy. It includes the exception name, category, line number of the error, etc.$ErrorView = 'NormalView' Get-ChildItem C:\NoDirectoryOutputGet-ChildItem : Cannot find path 'C:\NoDirectory' because ... Read More
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
PowerShell version 7 has introduced a few new null operators. They are as below.Null-Coalescing operator - ??Null Conditional Assignment Operators - ??=Null Conditional member access operators - ?. and ?[]a. Null-Coalescing operator - ??Null Coalescing operator ??evaluates the left-hand side condition or operand and if it is null then evaluates the right-hand side operand otherwise provides the value of the left-hand side operand.For example, Without the Null-Coalescing operator, we would have written a script like as shown below, $Name = $null if($Name -eq $null){"Name is Null"} Else {"PowerShell"}The above same condition can be written with the ?? operator.$name = $null ... Read More
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
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
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
The validateCount attribute in PowerShell function is to validate the length of an array, which means that you can pass the specific number of arguments into the parameter. In the below example we need array should contain a minimum 1 and maximum 4 values when we pass the values. For that, we will write the below script, Function ValidateArray { Param ( [ValidateCount(1, 3)] [String[]]$Animals ) return $PSBoundParameters }OutputPS C:\> ValidateArray -Animals Cow, Dog, Cat Key Value --- ----- Animals {Cow, Dog, Cat}The above output is valid but when we pass ... Read More
The ValidateScript attribute is to validate the script before entering inside the function. For example, let say you want to validate the path of the file, validate the remote computer connectivity, etc. We will take here remote server connectivity example.Without the ValidateScript attribute, we would have written the script as shown below.Function Check-RemoteServer { param ( [string]$Server ) if(Test-Connection -ComputerName $Server -Count 2 -Quiet -ErrorAction Ignore) { Write-Output "$server is reachable" } else { Write-Output "$Server is unreachable" } }OutputPS C:\> Check-RemoteServer -Server asde.asde asde.asde is ... Read More
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP