Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
11 views9 pages

Basic Elements of PHP

This document describes several basic elements of PHP such as variables, operators, arrays, control structures, comments, strings, and functions. It explains concepts such as variables that store data, operators that perform calculations, arrays that store lists of data, control structures that direct the flow of the program, and functions that group reusable blocks of code.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views9 pages

Basic Elements of PHP

This document describes several basic elements of PHP such as variables, operators, arrays, control structures, comments, strings, and functions. It explains concepts such as variables that store data, operators that perform calculations, arrays that store lists of data, control structures that direct the flow of the program, and functions that group reusable blocks of code.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

BasicelementsofPHP

PHP has several basic elements, here I list some for you.
basic concepts of PHP:

Variable: A variable is a memory space where it


will store a data that can later be used in your
code. We will explore this topic in more depth in the section 'Variables'
and types of variables" from the basic PHP course.

Operators: Operators are constructs that behave


generally as functions, but that differ syntactically or
semantically of the usual functions. The simple examples
the most common include arithmetic (addition with +), comparison (with >)
and logical operations (such as AND or &&).

Arrays: Arrays or matrices are special variables that store


a list of data within them. We will study the
arrays in the chapters 'Simple Arrays' and in 'Arrays (or matrices)'
(matrices) with keys.

Control structures: A control structure is a block of


programming that analyzes the variables and chooses a direction in which
go according to certain parameters.

PHP Comments: They are pieces of code that help you to


comment on what you are writing, so you can distinguish certain
parts of your code or even understand much better what you're going
programming. I recommend that you always use comments in your
code, every programmer should make this activity a habit
write your code. Here I show you how to make comments in PHP.
Howcanwedefineavariablein
PHP?
There are some basic rules you should follow when choosing a name for
your variables in PHP programming language.

A variable in PHP is distinguished from the rest of the code because


start with a dollar sign ($). If you don't declare your variable this way, PHP
he will never recognize it as such, so technically your variable
it will never exist within the PHP code, so you won't be able to work
with her.

Operators used by PHP


We can use simple arithmetic operators to add, subtract or
concatenate some variables.

We can also print variables in PHP using


the echo command.

For example, let's sum two numbers, let's put the result in
a new variable and print the result.

Other types of operators

In PHP we can use different operators, here is a list of


they.

Operator precedence: The precedence of an operator


specifies how it 'closely' binds two expressions together. By
for example, in the expression 1 + 5 * 3, the answer is 16 and not 18 because
the multiplication operator (‘*’) has a higher precedence than
the addition operator ('+'). Parentheses can be used to enforce
the precedence, if necessary. For example: (1 + 5) * 3 evaluates to 18.

Arithmetic operators: They are the same operators that we saw


In school, these are: identity, negation, addition,
subtraction, multiplication, division, the modulus (the modulus you
shows the remainder of a division), and exponentiation.

Assignment operators: The assignment operators in PHP are


they use numerical values to write a value in a variable. The
the basic assignment operator in PHP language is '='. It means that the
the left operand is set to the value of the expression of
assignment to the right. Here in the following code I show you all
the assignment operators in PHP.

Bitwise operators: Bitwise operators allow the


evaluation and manipulation of specific bits within a
whole.

Comparison operators: The comparison operators,


As their name indicates, they allow you to compare two values.

Error control operators: PHP supports an operator


error control: the at sign (@). When placed before a
expression in PHP, any error message that may be
generated by that expression will be ignored.

Execution operators: PHP supports an operator of


execution: backticks (" ).

Increment/decrement operators: The increment and decrement operators

PHP increments are used to increase the value of a


variable, while the decrement operators of PHP are
they are used to decrease the value of a variable.
Logical operators: PHP logical operators are used
to combine conditional statements.

String (or chain) operators: There are two string operators


chains. The first is the concatenation operator ('.'), which
returns the concatenation of its arguments to the right and
to the left. The second is the assignment operator of
concatenation (' .= '), Which adds the argument on the side
right to argument on the left side.

Array (or matrix) operators.

String format
Like Perl, PHP strings in double quotes
You can format strings using defined variables. For
example:

What is an array or matrix in programming language?


PHP?
Arrays are a special type of variable that can contain
many variables within itself, and at the same time keep them in one
kind of list.

Usefulfunctionsofarraysormatrices
The count function will return the number as a result.
of members who have an array within their structure.
The PHP function reset gets the first element of the array.
(Although it also resets the internal iteration pointer). Here is a
example of the reset function:

The function gets the last member of the array list.

What is a string in PHP?


A string is a sequence of characters, like 'Hello,'
world!" or the name of a person, thing, or city for example.

A string in PHP is a sequence of characters.


where a character is equal to a byte. However, before that
you can use your first string in PHP you have to create it
Firstly. There are different ways to do this.

The easiest way to define a string is to enclose it in quotes.


double $string = 'value', and all the characters between the quotes are
they will perceive as characters for the PHP code.

We can simply say that strings or character arrays are variables.


that contains text. For example, a string that contains a
name is defined as follows:

What is the switch structure?


The switch control structure is a good alternative to the
If/Else if/Else statements in situations where verification is desired
multiple values against a single variable or condition. It is really
effective if you have a variable that can have different cases, if you
we see this way we can say that it is preferable to use a
switch structure before using an if/elseif/else structure, since you
you will avoid writing so much code and you won't get tangled up in the structures
nested if statements; so you must study well for what cases you want
use an if/elseif/else structure or a switch structure. Here you
I show the basic syntax of a switch control structure:

What is the for loop?


For loops or 'para' loops are simple loops that help us iterate.
about an iterable variable using an index. There are two
types of for loops: a simple loop (C style) for loop, and a loop
foreach. Normally the for loop is used when you know about
in advance how many times the script should be run.

forloop
For loops are very useful when we need to iterate over a
matrix and referring to the member of the matrix using an index
changing. For example, let's say we have a list of numbers
odds. To print them, we need to refer to each element
individually. The code we write in the for loop can use the
index i, which changes in each iteration of the for loop. Here I show you the
example:

The initialization statement: in our case, we initialize the


iterator variable$ia0.

The condition statement: this statement is evaluated at each


cycle. The cycle stops when this condition is not met. This
it will happen when the iterator variable$ is greater than the
length of the matrix.

The increment/decrement instruction: this instruction is


execute in each iteration to increase the index variable in the
necessary amount. Generally, we will increase $ien1. Also
There are two shorter ways to increase a variable by 1.
We can use $i+=1 or we could also use the expression $i+
what is the most used.

What is the while loop (or while) in


PHP?
Often, when you write code, you want the same block of
code to execute over and over in a row. Instead of adding several
almost identical lines of code in a script, we can use loops to
carry out a task like this.

In PHP, the function of a while loop is to perform a task, over and over again,
whenever the specified conditional instruction is true.
So we can say that in this case PHP executes a block of
code while the condition is met over time.

While loops are simple blocks that execute repeatedly.


until its condition is no longer met.

What is a function in PHP?


Functions are simple blocks of code that we can call.
from anywhere in a PHP document, or even store them
in another external document and make a reference to the document in the
that we require them with a simple line of code. For example,
we can create a function that sums a list of numbers and
return the result. Let's call this function sum.
Class: This is a data type defined by the programmer, which
it includes local functions as well as local data. You can think of a
class as a template to create many instances of the same type
(or class) of object.

Object: an individual instance of the data structure defined by


a class. A class is defined once and then instances are created.
many objects that belong to her. The objects also
they know it as instance.

Member Variable: These are the variables defined within


a class. These data will be invisible to the outside of the class and will
you can access them through the member functions. These
Variables are called object attributes once an object is created.

Member function: they are the function defined within a class and
are used to access object data.

Inheritance: When a class is defined by inheriting a function


existing from a parent class, then it is called inheritance. Here the
the child class will inherit all or some of the functions and variables from
a class of parents.

Parent class: A class that is inherited from another class. This


It is also called base class or superclass.

Inherited class: A class that inherits from another class. This also
denomination subclass or derived class.

Polymorphism: This is an object-oriented concept where the


the same function can be used for different purposes. For
for example, the name of the function will remain the same, but
can take a different number of arguments and perform a task
different.
Overloading: It is a type of polymorphism in which some or all of the
operators have different implementations depending on the
type of their arguments. Similarly, the functions also
they can be overloaded with different implementations.

Data abstraction: Any representation of data in which


the implementation details are hidden (abstract).

Encapsulation: It refers to a concept where we encapsulate everything


the data and functions of the members together to form an object.

Constructor: refers to a special type of function that will be called


automatically every time there is an object formation of a
class.

Destructor: refers to a special type of function that will be called


automatically whenever an object is deleted or goes out of scope
of application.

You might also like