PHP Bootcamp: Part 4 – Strings, Constants, Operators, And If/Else Statements

18th December, 2017 by

In this article we will be focusing on how text write out functions can be used, as well as showing the potential of if/else/elseif statements. They can be really useful for combining two situations that counteract each other!

Strings

So far we have used Strings for several different functions, primarily for printing out text. As they are so prevalent, there are some important things to keep in mind when using them. Here are some helpful functions that can be used within it:

PHP strlen(): returns the length of a string.

PHP str_word_count() function counts the number of words in a string.

PHP strrev() function reverses a string.

PHP strpos() function searches for a specific text within a string.

PHP str_replace() function replaces some characters with some other characters in a string.

 

These are, of course, just some of the basic ones that are used a great deal. There are many more functions that are part of the PHP core, as well as others that can be modified in or custom scripted… the possibilities are endless!

Constants

These are pretty much what their names indicate, in that they remain constant and can’t be changed as a identifier once placed. They also start with a letter or underscore. The format of constants is as follows:

PHP Bootcamp: Part 4 - Strings, Constants, Operators, and if/else statements

As shown in this example, the parameters are set to the following values:

name: Specifies the name of the constant
value: Specifies the value of the constant.
case-insensitive: Specifies whether the constant name should be case insensitive. It is set to be false unless stated otherwise.

Here is an example of how these work:

PHP Bootcamp: Part 4 - Strings, Constants, Operators, and if/else statements

Here you’d be able to see “This is an example of a constant!” print off to the screen. “Introduction” is set to be the variable that stores this statement, as shown with the echo statement. One last thing to consider is that Constants can actually be placed outside of certain functions and still work. This is due to them being globally accessible, a special trait that not all functions share.

Operators

Operators serve the purpose of mostly math designations. In PHP, the operator is considered the same as the arithmetic symbols “+” and “-”  while the operand would be considered the value used for the arithmetic equation.

Increment and Decrement Operators

These are used to increase and decrease the variable value.

++$X: This is used to increase the value of a variable by one.

–$X: This is used to decrease the value of a variable by one.

Array Operators

This type of operator is used for comparing sets of arrays.

== (Equality): This returns the value “True” if both are equal variables.

=== (Identity): This returns the value “True” if both are equal variables as well as share the same order and type.

!== (Non-identity): If both value types are not identical to each other, then the operator returns the value “False”.

Comparison Operators

These are basically what the name means; they are used to pair with the operands to compare variables.

TIP: Both symbols “<” and “>” are used in PHP as well but for an unconventional purpose. It returns the value “True” and “False” if the left operand is respectively less than the right, or the left operand is greater than the right.

< : The operator returns “True” if the left operand is less than the right operand.

> : The operator returns “True” if the left operand is greater than the right operand.

If/Else/Elseif

When writing code, you want to be able set your conditions with how you organize your functions. This means if you want one situation to happen but not another, this is what you would use. Here is the syntax rules for all of these:

if (condition) {
   code to be executed if condition is true;
}

else {
   code to be executed if condition is false;
}

elseif (condition) {
   code to be executed if this condition is true;

}

Here is an example of these being used in conjunction together:

PHP Bootcamp: Part 4 - Strings, Constants, Operators, and if/else statements

The following code will print out “a is bigger than b, a equal to b or a is smaller than b”,

and will print out true as long as a is not greater than b. If a is greater than b then it is false

So if “a is equal to b” is FALSE then the second statement would be true and if both are wrong you get “a is not equal to b”.

Hopefully these techniques will give you a basis to start incorporating more extensive coding since If/Else/Elseif statements form the core of organizing the behaviors of the expression variables.

(Visited 50 times, 1 visits today)