PHP Keywords, Variables, Strings, Comments, and Output Processors Guide

Keywords

words with special meaning in the language which are reserved and cannot be used for any other purpose except declaring the meaning inscribed in the keyword. These keywords are integral to PHP syntax and are used to define control structures, functions, classes, variables, and more within PHP scripts.

Here is a list of PHP keywords:

  1. Abstract: Declares an abstract class or method.
  2. And: Logical AND operator.
  3. Array: Creates an array.
  4. As: Used in foreach loops to iterate over arrays and objects.
  5. Break: Exits the current loop or switch statement.
  6. Case: Specifies a condition in a switch statement.
  7. Catch: Catches exceptions in a try…catch block.
  8. Class: Defines a class.
  9. Clone: Creates a copy of an object.
  10. Const: Defines a constant.
  11. Continue: Skips the current iteration of a loop and continues to the next one.
  12. Declare: Specifies directive options for a block of code.
  13. Default: Specifies the default case in a switch statement.
  14. Do: Executes a block of statements repeatedly in a loop while a condition is true.
  15. Echo: Outputs one or more strings.
  16. Else: Executes a block of code if the condition is false in an if…else statement.
  17. Elseif: Executes a block of code if the first condition is false and the elseif condition is true in an if…elseif…else statement.
  18. Empty: Checks whether a variable is empty.
  19. Enddeclare: Ends the declaration block for declare.
  20. Endforeach: Ends a foreach loop.
  21. Endif: Ends an if statement.
  22. Endswitch: Ends a switch statement.
  23. Endwhile: Ends a while loop.
  24. Eval: Evaluates a string as PHP code.
  25. Exit: Terminates script execution.
  26. Extends: Indicates that a class inherits from another class.
  27. Final: Prevents child classes from overriding a method.
  28. Finally: Specifies a block of code to execute after a try…catch block.
  29. For: Initiates a for loop.
  30. Foreach: Initiates a foreach loop.
  31. Function: Defines a function.
  32. Global: Declares a variable as global inside a function.
  33. Goto: Transfers program control to another part of the code.
  34. If: Executes a block of code if a condition is true.
  35. Implements: Implements an interface in a class.
  36. Include: Includes and evaluates a specified file.
  37. Include_once: Includes and evaluates a specified file once during the script execution.
  38. Instanceof: Checks whether an object is an instance of a specific class.
  39. Insteadof: Used in trait composition to exclude conflicts between methods.
  40. Interface: Declares an interface.
  41. Isset: Checks whether a variable is set and is not NULL.
  42. List: Assigns variables as if they were an array.
  43. Namespace: Declares a namespace.
  44. New: Creates new objects.
  45. Or: Logical OR operator.
  46. Print: Outputs one or more strings.
  47. Private: Declares a class member as private.
  48. Protected: Declares a class member as protected.
  49. Public: Declares a class member as public.
  50. Require: Includes and evaluates a specified file.
  51. Require_once: Includes and evaluates a specified file once during the script execution.
  52. Return: Exits a function and specifies a value to be returned to the calling function.
  53. Static: Declares class members as static.
  54. Switch: Selects one of many blocks of code to execute.
  55. Throw: Throws an exception.
  56. Trait: Declares a trait.
  57. Try: Implements error handling for a block of code.
  58. Unset: Deletes a variable.
  59. Use: Imports namespaces or traits.
  60. Var: Declares a property in a class (deprecated in favor of public, protected, or private).
  61. While: Executes a block of code while a specified condition is true.
  62. Xor: Logical XOR operator.

Variables

They are place holders for values that are dynamic in the program. variables are memory locations that are likely to change during program execution.

In php, variables are preceded by a dollar sign($) followed by a letter or underscore, followed by any number of letters , numbers or underscore.

variables in php are case sensitive.

Strings

Strings are literals you can use between single quotes or double quotes to display a literal meaning on screen. What is between quotes is displayed as it is after program execution.

Backslash (\) is used as an escape character

String can span multiple lines where each new line is considered part of the string.

Concatenation can be done on php strings using a dot(.). For example “I am the”. ” Chairman of the department” are two strings joined together by the dot.

comments

They are part of the code in the program that program does not intend them to be interpreted by the processor but are supposed to be ignored during execution. They are useful in program execution and debugging

in php we have two types of comments.

C++ comments (//) that comment a single line by being at the starting starting point of a statment

Multiple line comments (/* */ ) that enables a comment to span a number of lines.

pearl/shell comment (#)

output processors

echo

It is a language construct that can be treated like a function with one parameter. It is presented without parenthesis and it accepts multiple parameters. e.g. echo $x, \n, “dollars”;

print

It is a function that accepts only one parameter but with parenthesis that are optional so as it can look like a language construct