What I’m Learning- PHP Constants and Operators

Are you looking to use Constants and Operators in your PHP code? Here is a  collection of some things you will need.

Constants

A constant is something that will not be changed, starts with a letter or _ (no $), and can be used throughout script.

Start here:

define(name, value, case-insensitive)

Name: The name of the constant you are creating

Value: The value of the constant you are creating

Case Insensitive: Default is false (meaning if you want to be case insensitive, don’t fill in this portion)

Unlike strings, constants can be inside or outside functions.


Operators

Perform operations on variables and values (words and numbers) in the following groups:

  • Arithmetic operators
    • Simple math problems
    • <?php
      $x = 10;  
      $y = 6;
      echo $x + $y;
      ?> 
  • Assignment operators
    • using = like “x=y+z”, the x is now equal to the equation
    • <?php
      $x = 20;  
      $x += 100;
      echo $x;
      ?>  
  • Comparison operators
    • are used to compare two values
    •  Got this from W3Schools.com, great site for all languages.
  • Increment/Decrement operators
    • The PHP increment operators are used to increment a variable’s value.

      The PHP decrement operators are used to decrement a variable’s value.

  • Logical operators
  • String operators
  • Array operators

Cheers,

The Code Damsel

 

What I’m Learning – PHP Special String Commands

Below I have string commands that are used in the PHP language that will allow you to find out information or change what is already written!

The following string command will return how many characters are within the string:

<?php
echo strlen(“Hello world!”); // outputs 12
?>

The following string command will return how many words are within the string:

<?php
echo str_word_count(“Hello world!”); // outputs 2
?>

The following string command will return the string written backwards:

<?php
echo strrev(“Hello world!”); // outputs !dlrow olleH
?>

The following string command looks for a specific word, and will return the location within the string. Unless it is not there, then will return “false”:

<?php
echo strpos(“Hello world!”, “world”); // outputs 6
?>

The following string command will replace the first word with the second word, in the third section:

<?php
echo str_replace(“world”, “Monkey”, “Hello world!”); // outputs Hello Monkey!
?>
Hope this was helpful,
The Code Damsel