Friday, October 9, 2020

PHP Short Note Part 1 (PHP Basics)

This is a short note that I created to refresh my memory on for the introduction to PHP and I hope to continue while I keep learning more about this serve side scripting language. 

PHP also is known as Hypertext Pre Processor code executes on the server and the results return to the browser as plain HTML. The PHP files should have a “.php” extension. PHP can generate dynamic page content which allows the server to generate unique content each time the page is loaded. PHP can perform many tasks on files such as open, read, write, create, close files in the server as well as add delete, modify data in the database. It is also used to control user access and encrypt data. 


Syntax

- The sript should start and end as <?php                          ?>

- The statements in PHP should end in ' ; '  sign ex: echo “Hello PHP”;

- PHP keywords (if, else, etc.) classes, functions, and user-defined functions are not case sensitive.

            Ex: echo    , right Echo 

However, all the variable names are case sensitive.

Ex: $test and $Test are not the same

Comments

These are used to let others understand the code better and for you to help remember easily what that part of the code does.

// - for a single comment

# -  for a single comment

/*      */ Multiple line comment

Variables 

Variables are used to store data and start with a ‘$’ sign followed by the name. It is created when you assign a value for it. As stated above variables are case sensitive.

$temp = “Hello”;

$b = 35.3;

A variable must start with a letter or an underscore character and cannot start with a number.

Should only contain: A-z , 0-9 and _

1. Local 

A variable within a function.

2. Global

A variable outside the functions. PHP uses the “global” keyword to access a variable within a function. An array called $GLOBALS[index] stores all global variables and can be used to update global variables directly.


3. Static

When a function executes, all its local variables get deleted. Using “Static” keyword you can prevent that.



Echo and print statements

Both of these statements are used to output data o the screen. Both can be used with or without parenthesis ().





Data Types

PHP supports the following  8 data types.



var_dump() function can be used to return the data types and values.


Commonly used PHP String functions



PHP Numbers and Math

Following are some functions that can be used on Integers, Floats, and Number Strings.


Next we’ll see some PHP functions that is used to perform simple mathematical tasks.


PHP Constants

Constants are lot like variables that cannot change or be undefined. They can be used across the script as they function in global scope. To create a constant,

Define(name, value, case sensitivity)


You can also create constant arrays.

Ex: define( “FRUITS”[“Mango” ,”Peach”, ”Orange”] )


References

https://www.w3schools.com/php/default.asp

Authors Note

It's been a while since I last coded in PHP. But I always liked the language. w3schools has been the main site where I learned my basics and I created this short note referring to that site. If you need a more elaborate explanation it is the place to go. This article is mainly written as a quick read to refresh the memory.


No comments:

Post a Comment

Review of California Consumer Privacy Act (CCPA) and the amendment of California Privacy Rights Act (CPRA)

California Consumer Privacy Act (CCPA) became effective on 1st of January 2020 enhancing the privacy rights and consumer protection for the ...