PHP for Beginners – Part 1



php-for-beginners

This tutorial is intended for people having a fair bit of knowledge in programming, and wants to learn PHP. I will be illustrating the basics and key points followed by working demo which are utmost necessary in learning the subject. Before we get going make sure you have installed the proper enviroment to run PHP or you can run some demo test on phpfiddle. But I strongly suggest you to install Xampp or Wamp or Mamp on your machines.

Spend a considerable amount of time learning the basics, will help you scratch your head less when you go through the advance part.

What is PHP

PHP (PHP: Hypertext Preprocessor) is a server-side scripting language, which can be embedded in HTML or used as a standalone binary.

How PHP works

Just like a .html or .txt file we create a .php file. Unlike an ordinary HTML file, a PHP script is not sent directly to a client by the server, instead, it is parsed by the PHP engine. HTML elements in the script are left alone, but PHP code is interpreted and executed. PHP code in a script can query databases, create images, read and write files, talk to remote servers—the possibilities are endless. The output from PHP code is combined with the HTML in the script and the result sent to the user.

PHP Tags

Let us start writing out first php program. All php script is written under php tags. Have a look.

You can also use the short-open tags.

Note: I prefer using the first one since it safe and always guarantee me the tags will be correctly interpreted. In using the second case that it is not compatible with XML. If you still want to use you can can go to php.ini file (located under xampp/php folder) and uncomment short_open_tag.

Using Comments

A comment is the portion of a program that exists only for the human reader. Comments can be used where you want to explain something to other or probably yourself about the section of the code. The comments will never be executed by the script.

Variables

Definition: A variable is an identifier that denotes a storage location used to store data value.

Explanation: A variable is like a container that has a name and is used to store some data. Variable is most basic thing that you will be using whenever your code. Since you will always have some data to process, variable is where they get stored during the course of execution of the script.

Follow these rules while naming the variables:

  • All variables in PHP are denoted with a leading dollar sign ($).
  • Variables are assigned with the = operator, with the variable on the left-hand side and the expression to be evaluated on the right.
  • Variables can, but do not need, to be declared before assignment.
  • Variables names must not start with a digit, Eg $123

Where to use variables: Almost everywhere, whenever you need to store some value, you will be needing variable.

Constants

Definition: Constants refers to the fixed value that don’t change during the course of execution of the script.

Explanation: Whenever you think about constant, think about Eiffel Tower or some thing similar to that which is fixed and not going to change, or a value that you want to be fixed and not to be changed.

Where to use constants: Lets take a scenario where you need to store π(pie) value a fixed value to some container, or where you want to use your SSN number or website address which you don’t want to be changed.

Printing Output to screen

To display the result on the browser or other output device PHP has its own in built function. Lets have the look at them.

Data Types

Definition: Data types refers to types of data that can be stored that in a variable.

Explanation: Let’s think this way, Human Being, Animals, Insects all have their own characteristics, which differentiates one from the others, same is the case for data type. They can take data to their range and perform operations accordingly. PHP has a total of eight types: integers, doubles, Booleans, strings, arrays, objects, NULL, and resources

  • Integers are whole numbers, without a decimal point, like 495.
  • Doubles are floating-point numbers, like 3.14159 or 49.0.
  • Booleans have only two possible values: TRUE and FALSE.
  • NULL is a special type that only has one value: NULL
  • Strings are sequences of characters.
  • Arrays are named and indexed collections of other values.
  • Objects are instances of programmer-defined classes, which can package up both other kinds of values and functions that are specific to the class.

N.B: By Default PHP automatically sets a data type for a variable.

Type Casting

It refers to the conversion of a data type to another. By default PHP converts the type implicitly.

Operators

Definition: Operators is the symbol that tells the computers to perform certain mathematical or logical manipulation.

Explanation: Let’s think like way, Operators sets the relation between variables. Take a look at some of operators.



Arithmetic Operators

Operators Operation It Performs Example Results
+ Addition Explicit positive sign 12 + 43 55
Subtraction Negation 20 – 5 – 3 15
* Multiplication 3*4 12
/ Division 5/2 2
% Modulo division 5%2 1
++ Post-increment Pre-increment ++a ++b increments value by one
Post-decrement Pre-decrement –a –b decrements value by one

Assignment Operators

Operators Operation It Performs Example Results
= Assign right side to left side $a = 13 $a has value 13
+= Add right side to left side $a += 2 $a = $a + 2
-= Subtract right side to left side $a -= 2 $a = $a – 2
*= Multiply right side to left side $a *= 2 $a = $a * 2
/= Divide right side to left side $a /= 2 $a = $a / 2
%= Set left side to left side modulo right side $a %= 2 $a = $a % 2

Comparison Operators

Operators Operation It Performs Example Results
< less than 5 < 6 false
> greater than 25 > 16 true
<= less than or equals to 15 <= 5 false
>= greater than or equals to 25 >= 25 true
== equals to 5 == “5″ true
=== Identical 5 === “5″ false

Logical Operators

Logical operators combine other logical (aka Boolean) values to produce new Boolean values. We use it mainly in combination with comparison operators

Operators Operation It Performs Example Results
&& and (8>6) && (2>3) false. both relation must be true
|| or (8>6) && (2>3) true, if any one of them is true.
! not !false true, Is true if its single argument (to the right) is false and false if its argument is true.
xor exclusive or (8>6) && (2>3) true, Is true if its single argument (to the right) is false and false if its argument is true.
and same as &&
or same as ||

This is the end of this part, Next part of the series will be Published soon, Till then enjoy the code 🙂

 



Was this article/content helpful?
Thanks! Your feedback helps us to improve PHPHive.info

Leave a Reply

Your email address will not be published. Required fields are marked *