how to learn php – Cyber Hyena https://cyberhyena.net web security crypto currencies and learn programming Tue, 16 Mar 2021 18:38:44 +0000 en-US hourly 1 https://wordpress.org/?v=5.8.6 https://cyberhyena.net/wp-content/uploads/2020/11/New-Project-2020-11-24T165858-150x126.png how to learn php – Cyber Hyena https://cyberhyena.net 32 32 free PHP programming course E5 learn to code https://cyberhyena.net/blog/2021/03/16/free-php-programming-course-e5-learn-to-code/ https://cyberhyena.net/blog/2021/03/16/free-php-programming-course-e5-learn-to-code/#respond Tue, 16 Mar 2021 18:38:44 +0000 https://cyberhyena.net/?p=268 FREE php course Lecture #5: Selection Structure  Any PHP script is built out of a series of statements. A statement can be an assignment, a function call, a loop (discussed in a later lecture), a conditional statement, or even a statement that does nothing (an empty statement). Statements usually end with a semicolon. In addition, […]

The post free PHP programming course E5 learn to code appeared first on Cyber Hyena.

]]>
0 0
Read Time:14 Minute, 55 Second

FREE php course Lecture #5: Selection Structure  Any PHP script is built out of a series of statements. A statement can be an assignment, a function call,
a loop (discussed in a later lecture), a conditional statement, or even a statement that does nothing (an
empty statement). Statements usually end with a semicolon. In addition, statements can be grouped
into a statement-group by encapsulating a group of statements with curly braces. A statement-group is
a statement by itself as well.

Control Statements … FREE php course

A control statement is a statement that is used to alter the continuous sequential invocation of
statements. It is a statement that controls when and how one or more other statements are executed. In
PHP, statements fall into three general types:

Assignment, where values, usually the results of calculations, are stored in variables (as discussed
in previous lecture).
Input / Output, data is read in or printed out.
Control, the program makes a decision about what to do next.

Unless being specified, statements in a PHP program are executed one after another in the order in
which they are written, this is known as sequential execution. However, programs can be much more
useful if the programmer can control the sequence in which statements are run. You will soon find out
that there will always be a need to jump off the linear sequence. A transfer of control occurs when the
next statement to be executed are specified by the programmer, instead of the immediate succeeding
statement.

As a programmer, the control of statement sequence is actually the arrangement of:

• selecting between optional sections of a program.
• repeating important sections of the program.

All PHP programs are thus written in terms of three forms of control: sequence, selection, and
repetition.

Conditional Statements

A conditional statement is used to decide whether to do something at a special point or to decide
between two courses of action. A conditional statement chooses among alternative actions in a PHP
program. The if statement performs (selects) an action based on a tested condition.

A condition is an expression with a Boolean value (TRUE or FALSE) that is used for decision making. For example,

$x >= 3

In this statement, the value of variable $x can be anything, such as 1, 2, 8, 110, 224, and so on. This
expression is then sometimes true, sometime false depending on the current value of $x .

Conditional statements can be as simple as the above one or they can be compound statements as
shown below:

($x >= 3) AND ($x <= 15)

For example:

<?php
$s = 6;
$t = 5;
if ($s>4 AND $t>4) {echo “TRUE!”; }
?>

The output is TRUE! because both conditions $s>4 and $t>4 are true.

 

The if statement …..,, FREE php course

The if statement is one of the most important features of many languages, PHP included. It allows for
conditional execution of code fragments. PHP’s if statement is similar to that of the C programming
language.

In PHP, many conditional statements (also known as selection statements) begin with the keyword if,
followed by a condition in parentheses.

The statement(s) of execution are grouped together between curly brackets { }. Such a grouping is called a compound statement or simply a block. The syntax
then looks like:

if ( condition )
{ execution }

where condition is an expression that is evaluated to its Boolean value. If expression evaluates to
TRUE, PHP will execute the statement, and if it evaluates to FALSE, PHP ignores it.
The following test decides whether a student has passed an exam with a passing mark of 60. Notice
that it is possible to use the if part without the else part.

<?php
$grade=rand(50,99); // generate a random number from 50 to 99
if ($grade >= 60) // test if the value of user entry >= 60
{ echo “Pass!”; } // display Pass!
?>

The following example checks whether the current value of the variable $hour is less than 12 and the
echo statement displays its message only if this is the case (namely $hour < 12 is true).

<?php
$hour = date(“H”); //get the current hour value
if ($hour < 12) {
echo “Good morning!”;
}
?>

The if … else statement

The above example has a logic problem. It specifies what to execute when the $grade is greater than or
equal to 60, but it did not specify what to do if the $grade is less than 60. To solve this logic problem,
use the if … else statement. The if statement performs an indicated action (or sequence of actions) only
when the condition is tested to be true; otherwise the execution is skipped. The if … else statement
performs an action if a condition is true and performs a different action if the condition is false.
The else statement extends an if statement to execute a statement in case the expression in the if
statement evaluates to FALSE. The syntax looks like:

if ( condition )
{ execution 1 } // do this when condition is TRUE
else
{ execution 2 } // do this when condition is FALSE

You can now rewrite the above example to:

<?php
$grade=rand(50,99); // generate a random number from 50 to 99
if ($grade >= 60) // condition
{ echo “Pass!”; } // execution 1
else
{ echo “Fail!”; } // execution 2
?>

 

The else statement is only executed if the “if” expression is evaluated to FALSE as well as any elseif
expressions that evaluated to FALSE.

Often you need to have more than one statement to be executed conditionally. Of course, there is no
need to wrap each statement with an if clause. Instead, you can group several statements into a
statement group.

If statements can be nested infinitely within other if statements, which provides you with complete
flexibility for conditional execution of the various parts of your program.

The elseif statement ….. FREE php course

Sometimes a programmer needs to make a decision based on several conditions. This can be done by
using the elseif variant of the if statement.

The elseif statement, as its name suggests, is a combination of if and else. Like else, it extends an if statement to execute a different statement in case the original if
expression evaluates to FALSE. However, unlike else, it will execute that alternative expression only if
the elseif conditional expression evaluates to TRUE.

As soon as one of these conditions yields a true result, the following statement or block is executed and
no further comparisons are performed.

There may be several elseif’s within the same if statement. The first elseif expression (if any) that
evaluates to TRUE would be executed. In PHP, you can also write else if (in two words) and the
behavior would be identical to the one of elseif (in a single word). The syntactic meaning is slightly
different, but the bottom line is that both would result in exactly the same behavior.
The elseif statement is only executed if the preceding if expression and any preceding elseif
expressions evaluated to FALSE, and the current elseif expression evaluated to TRUE.

In the following example we are awarding grades depending on the exam result.

<?php
$grade=rand(50,99); // generate a random number from 50 to 99
if ($grade >= 90)
{ echo “A”; }
elseif ($grade >= 80)
{ echo “B”; }
elseif ($grade >= 70)
{ echo “C”; }
elseif ($grade >= 60)
{ echo “D”; }
else
{ echo “F”; }
?>

In this example, all comparisons test a single variable called result. In other cases, each test may
involve a different variable or some combination of tests. The same pattern can be used with more or
fewer elseif’s, and the final lone “else” may be left out. It is up to the programmer to devise the correct
structure for each programming problem.

The if…then statement can also be used to handle HTML form in one single PHP script (a later lecture
will discuss this topic in details). For example:

<?php
if ($_POST) { // if the form is filled out
$x = $_POST[“vofx”];
if ($x>=5) { echo “x is greater than or equal to 5!”; }

else { echo “x is less than 5!”; }
}
else { // otherwise display the form
?>
<form action=”<?php echo $_SERVER[‘PHP_SELF’]; ?>” method=”post”>
Enter a value for <i>x</i>:
<input type=”text” name=”vofx” size=5>
<input type=”submit” value=”Check”></form></p>
<?php
}
?>

The $_POST expression is evaluated when the script is loaded to determine whether or not the form
fields (a textbox) has been filled in. If so, the script ignores the else part and processes the user input. If
not, the script displays only a form for the user to fill in.

if ($_POST) { // if the form is filled out
…… }
else { // otherwise display the form
?>
<form action=”<?php echo $_SERVER[‘PHP_SELF’]; ?>” method=”post”>
……
</form>
<?php
}
?>

Notice that it uses the $_SERVER[‘PHP_SELF’] system variable to send the user inputs to itself.

Alternative syntax ….. FREE php course

PHP offers an alternative syntax for some of its control structures. the basic form of the alternate
syntax is to change the opening brace to a colon (:) and the closing brace to endif. For example:

<?php if ($a == 5): ?>
A is equal to 5
<?php endif; ?>

In the above example, the HTML block “A is equal to 5″ is nested within an if statement written in the
alternative syntax. The HTML block would be displayed only if $a is equal to 5.

The alternative syntax applies to else and elseif as well. The following is an if structure with elseif and
else in the alternative format.

<?php
if($a > $b):
echo $a.” is greater than “.$b;
elseif($a == $b): // Note the combination of the words.
echo $a.” equals “.$b;
else:
echo $a.” is neither greater than or equal to “.$b;
endif;

?>

Note that elseif and else … if will only be considered exactly the same when using curly brackets as in
the above example. When using a colon to define your if/elseif conditions, you must not separate else if
into two words or PHP will fail with a parse error.

Problem of complexity

When writing a PHP program, you may have to compare the same variable (or expression) with many
different values, and execute a different piece of code depending on which value it equals to. You can
do so with nested if … else statements, but it will increase the complexity of your program. Consider
the following example:

<?php
$x= date(“w”); //number of the day of the week; 0 for Sunday
if ($x == 0) { echo “Sunday”; }
elseif ($x == 1) { echo “Monday”; }
elseif ($x == 2) { echo “Tuesday”; }
elseif ($x == 3) { echo “Wednesday”; }
elseif ($x == 4) { echo “Thursday”; }
elseif ($x == 5) { echo “Friday”; }
elseif ($x == 6) { echo “Saturday”; }
else { echo “No such option!”; }
?>

This above code will display different value according to the number of the weekday—0 for Sunday, 1
for Monday, and so on. If the number is not in {0, 1, 2, 3, 4, 5, 6}, then it will display “No such
option!”. This example use only one block of code to handle 8 conditions, which certainly increase the
complexity (imaging you have 20 conditions!). This is exactly what the switch statement is for.

The switch..case Statement

The switch statement is similar to a series of if statements on the same expression, but it is a better way
of writing a program when a series of if..else’s occurs. It is important to understand how the switch
statement is executed in order to avoid mistakes. The switch statement executes line by line (actually,
statement by statement). The switch/case statement syntax is as follows:

switch (expression) {
case value1:
statement(s);
break;
case value2:
statement(s);
break;
………..
………..
default:
statement(s);
break;
}

where the case expression may be any expression that evaluates to a simple type, that is, integer or
floating-point numbers and strings. Arrays or objects cannot be used here unless they are not
referenced to a simple type.

In a switch statement, the condition is evaluated only once and the result is compared to each case
statement. In an elseif statement, the condition is evaluated again. If your condition is more
complicated than a simple compare and/or is in a tight loop, a switch may be faster. The advantage of
using switch statement is to simplify the complexity of if…else’s.

The following two examples show two different ways to test for the same conditions – one uses a series
of if and elseif statements, and the other uses the switch statement.

It is important to understand how the switch statement is executed in order to avoid mistakes. The
switch statement executes line by line (actually, statement by statement). In the beginning, no code is
executed. Only when a case statement is found with a value that matches the value of the switch
expression does PHP begin to execute the statements. PHP continues to execute the statements until
the end of the switch block or the first time it sees a break statement. The break construct is used to
force the switch code to stop. PHP will continue to execute the statements until the end of the switch
block or the first time it sees a break statement. If you don’t include a break statement at the end of a
case’s statement list, PHP will go on executing the statements of the following case.

Consider the following examples;

FREE php course
FREE php course

Only when a case statement is found with a value that matches the value of the switch expression does
PHP begin to execute the switch statements.

Notice that a special case is the default case. This case matches anything that doesn’t match any other
cases, and should be the last case statement.

In PHP, the statement list for a case can also be empty, which simply passes control into the statement
list for the next case. The use of default case is also optional. In the following example, only when the
weekday is Tuesday will a user see an output.

 

<?php
$i= date(“w”);
switch ($i) {
case 0:
case 1:
case 2:
echo “It is Tuesday, and you just won the prize!”;
break;
case 3:
case 4:
case 5:
case 6:
}
?>

In PHP, the switch structure allows usage of strings. For example,

<?php
switch ($i) {
case “apple“:
echo “i is apple”;
break;
case “bar“:
echo “i is bar”;
break;
case “cake“:
echo “i is cake”;
break;
}
?>

The alternative syntax for control structures is supported with switches. For example:

<?php
switch ($i):
case 0:
echo “i equals 0”;
break;
case 1:
echo “i equals 1”;
break;
case 2:
echo “i equals 2”;
break;
default:
echo “i is not equal to 0, 1 or 2”;
endswitch;
?>

It is possible to use a semicolon ( ; )instead of a colon ( : )after a case. For example:
<?php
switch($beer)
{
case ‘Tuborg’;
case ‘Carlsberg’;
case ‘Heineken’;
echo ‘Good choice’;
break;
default;
echo ‘Please make a new selection…’;
break;

}
?>

 

Review Questions

1. The result of the following calculation is __.

<?php echo (9 != 7); ?>

A. 1
B. 0
C. True
D. False

2. The result of the following calculation is __.

<?php echo !(6 <= 4); ?>

A. 1
B. 0
C. True
D. False

3. Given the following code segment, what is the output?

<?php
$x = 3;
if ($x > 3) { echo “Correct!”; }
else { echo “Incorrect!”; }
?>

A. Correct!
B. Incorrect!
C. True
D. False

4. Given the following code segment, what is the output?

<?php
$x = 6;
if (($x > 3) AND ($x <=7)) { echo “Correct!”; }
else { echo “Incorrect!”; }

?>

A. Correct!
B. Incorrect!
C. True
D. False

5. The result of the following calculation is __.

<?php
$i=6;
if ($i>=4) {
if ($i=5) { echo $i; }
else { echo $i–;} }
else { echo $i++; }
?>

A. 4
B. 5

C. 6
D. 3

6. The result of the following calculation is __.

<?php
$i=3;
if ($i>=4) {
if ($i=5) { echo $i; }
else { echo $i–; } }
else { echo ++$i; }
?>

A. 4
B. 5
C. 6
D. 3

7. If the value of $i is 0, what is output?

<?php
switch ($i) {
case 0:
echo “i equals 0”;
break;
case 1:
echo “i equals 1”;
break;
case 2:
echo “i equals 2”;
break;
default:
echo “i equals 3”;
break;
}
?>

A. i equals 0
B. i equals 1
C. i equals 2
D. i equals 3

8. If the value of $i is 3, what is output?

<?php

switch ($i) {
case 0:
echo “i equals 0”;
break;
case 1:
echo “i equals 1”;
break;
case 2:
echo “i equals 2”;
break;
default:
echo “i equals 3”;
break;
}
?>

A. i equals 0
B. i equals 1
C. i equals 2
D. i equals 3

9. If the value of $i is 7, what is output?

<?php
switch ($i) {
case 0:
echo “i equals 0”;
break;
case 1:
echo “i equals 1”;
break;
case 2:
echo “i equals 2”;
break;
default:
echo “i equals 3”;
break;
}
?>

A. i equals 0
B. i equals 1
C. i equals 2
D. i equals 3

10. If the value of $i is 0, what is output?

<?php
switch ($i) {
case 0: echo “i equals 0”;
case 1: echo “i equals 1”;
case 2: echo “i equals 2”;
default: echo “i equals 3”;
}
?>

A. i equals 0
B. i equals 0i equals 1
C. i equals 0i equals 1i equals 2
D. i equals 0i equals 1i equals 2i equals 3

full lectures HERE

FREE php course

Happy
Happy
0 %
Sad
Sad
0 %
Excited
Excited
0 %
Sleepy
Sleepy
0 %
Angry
Angry
0 %
Surprise
Surprise
0 %

The post free PHP programming course E5 learn to code appeared first on Cyber Hyena.

]]>
https://cyberhyena.net/blog/2021/03/16/free-php-programming-course-e5-learn-to-code/feed/ 0
free PHP programming course E2 learn to code https://cyberhyena.net/blog/2021/03/12/free-php-programming-course-e2-learn-to-code/ https://cyberhyena.net/blog/2021/03/12/free-php-programming-course-e2-learn-to-code/#respond Fri, 12 Mar 2021 20:47:34 +0000 https://cyberhyena.net/?p=209 FREE php course Preparation #1: Build a testing Web Server 1. Use Internet Explorer to visit http://www.apachefriends.org/en/xampp-windows.html and download the latest installer of XAMPP for Windows. As of the time this lecture is written, the latest version is 1.8.0. 2. Launch the installer file (e.g. xampp-win32-1.8.0-VC9-installer.exe) read more about how to install xampp here  Learning […]

The post free PHP programming course E2 learn to code appeared first on Cyber Hyena.

]]>
0 0
Read Time:15 Minute, 0 Second

FREE php course Preparation #1: Build a testing Web Server
1. Use Internet Explorer to visit http://www.apachefriends.org/en/xampp-windows.html and download the
latest installer of XAMPP for Windows. As of the time this lecture is written, the latest version is 1.8.0.
2. Launch the installer file (e.g. xampp-win32-1.8.0-VC9-installer.exe)

read more about how to install xampp here 

Learning Activity #3: Basic PHP Start and End tags
1.
In the “X:\xampp\htdocs\myphp” directory, use Notepad to create a new file named lab1_3.php with the
following contents:

<html>
<?php echo “Display the word ‘Welcome’ using ‘&lt;?php … &gt;’!”; ?><br>
<? echo “Display the word ‘Welcome’ using ‘&lt;? … &gt;’!”; ?><br>
<script language=”php”> echo “Display the word ‘Welcome’ using ‘&lt;script&gt;’!”;
</script>
</html>

2. Use a Web browser to visit http://localhost/myphp/lab1_3.php, you should see the following window.

 

Learning Activity #4: Commenting your PHP code FREE php course

1.
In the “X:\xampp\htdocs\myphp” directory, use Notepad to create a new file named lab1_4.php with the
following contents:

<html>
<body>
<!– This line is an HTML comment –>
<?php
//This line is a single line PHP comment.
/*This is a multiple-line comment. Line 1
Line 2
Line 3 */
# This line is a Linux/Unix shell comment
echo “Do you see any of the comment?”;
?>
</body>
</html>

 

2. Use a Web browser to visit http://localhost/myphp/lab1_3.php, you should see the following window.

 

Learning Activity #5: Using HTML tag to format PHP output FREE php course

1.
In the “X:\xampp\htdocs\myphp” directory, use Notepad to create a new file named lab1_5.php with the
following contents:

<html>
<body bgcolor=”yellow”>
<b><?php echo “PHP Programming!” ?></b><br>
<u><?php echo “PHP Programming!” ?></u><br>
<i><?php echo “PHP Programming!” ?></i><br>
<s><?php echo “PHP Programming!” ?></s><br>
<h1><?php echo “PHP Programming!” ?></h1><br>
<pre><?php echo “PHP Programming!” ?></pre><br>
<center><?php echo “PHP Programming!” ?></center><br>
</body></html>

 

Use a Web browser to visit http://localhost/myphp/lab1_3.php, you should see the following window.

 

FREE php course
FREE php course

Lecture #2: PHP Variables, Data Types, and Expressions

Definition of data type

Human languages, such as English, use combinations of character to represent data. In English,
characters include alpha characters (lowercase and uppercase), numerals (0, 1, 2, .., 9), and symbols
($,#,*,&..). For example:
• Jennifer represents a girl’s first name, and it is a combination of alphabets.
• 714-852-9634 is a phone number, and it is a combination of numerals and symbol (-).
• D009865471 is a student ID, and it is a combination of alphabet and numerals
Different data can use different combinations of alpha characters, numerals, and symbols; therefore,
a data type is set of data with values having predefined characteristics and format. A data type is a
specification that assures the use of a specific set of values.
Usually a limited number of data types are built into a programming language. The language usually
specifies the range of values for a given data type, how the computer processes the values, and how
they are stored. In other words, the data type defines how computers should process a given variable
( of that data type).

 

What is a variable?

In programming languages, a variable is a language object that may hold different values for a period
of time. The value of a variable is usually restricted to a certain data type, such as an integer or
floating-point number.

A variable can be thought of as a programming construct used to store both numeric and non-
numeric data.In some cases, the contents of a variable can be changed during program execution. A
variable needs a name to be used to represent a data item whose value can be changed while the
program is running.

Variables in PHP are represented by the dollar sign prefix followed by the name of the variable. The
variable name is case-sensitive. For example:

$name;
$num;

Usually, the value of a given variable is provided by the user. For example, you enter your age to the
text box and then click Go. Your age will be assigned to a variable as value and is later retrieved and
inserted to the next sentence. You can keep on entering different numbers. See the example PHP
code below.

<?php
$age = $_POST[“age”];
?>

…….
<form action=”test.php” method=”post”>
<input type=”text” name=”age”>
…….

The above PHP example declares a variable with a valid name (e.g., $age) without assigning initial
value (meaning it has a null value). The “$age = $_POST[“age”];” line will accept whatever
value the user assigns to the $age through a form. When the user enters a value (e.g., 19), the user
indeed assigns a value to the variable dynamically.

PHP Expressions

PHP expressions are the building blocks of a PHP code. In PHP, almost any code you write is an
expression. The PHP manual defines a PHP expression as “anything that has a value”.

The most basic forms of expressions are constants and variables. When you type $a = 5, you assign

“5” to the variable, $a. In other words $a = 5 is an expression that the variable $a has the value of 5.
After this assignment, if you wrote $b = $a, you technically express your wish of $b = 5.

Some expressions can be considered as statements. In this case, a statement has the form of
expression;

namely, an expression followed by a semicolon. The following example is a expression and a valid
PHP statement.

$b = $a = 5;

Given an expression, $a = 5. There are two objects involved, the value of the integer 5 and the
variable $a which is being assigned with 5. It simply means that $a = 5. Regardless of what it does, it
is an expression that $a is given the value 5. Thus, writing something like “$b = $a = 5;” or “$b
= ($a = 5);” is equivalent to writing two statements: “$a = 5;” and “$b = 5;” (a semicolon
marks the end of a statement). Since assignments are parsed in a right to left order, you need to write
$b = $a = 5;    (or “$a = $b = 5;”).

Concept of using escaped sequence

PHP reserves many characters to help processing values held by variables, such as single quote (‘),
double quote (‘), dollar sign ($), and others. In other words, such reserved characters are not treated
as string literals as in plain English.

Sometimes it is necessary to display special PHP reserved characters as text. In this case, you will
then need to escape the character with a backslash (\). PHP uses the backslash (\) character to
restore and/or redefine characters that otherwise have a special PHP meaning, such as the newline
backslash itself (\n), or the quote character. The restoration/redefinition is known as a “escape
sequence “.

In many languages, escape sequences are used to format values of variables.

 

For example, review the following PHP code:

<?php
echo “A newline starts here \n”;
echo “A carriage return is \r”;
echo “A tab is \t.”;
echo “A dollar sign is \$”;
echo “A double-quote is \””;
?>

The output should be:

A newline starts here
A carriage return is
A tab is
.A dollar sign is $A double-quote is ”

Important Note!

There is a critical problem with using an escaped sequence with web browser–the web browser will
simply ignores blank spaces. And that explains why you did not get the above results when you test
them with web browser. You can use the “View Source” function of the browser to view the correct
output.

Basic PHP data types

PHP supports a number of different data types. The common ones are integers, floating point
numbers, strings, and arrays. In many languages, it’s essential to specify the variable type before
using it.

For example, a variable may need to be specified as type integer or type array. A list of
some of the PHP data types and their definitions are:

• integer: An integer is a plain whole number like 35, -80, 1527 or 1. They cannot have fractional
part.
• floating-point: A floating-point number is typically a fractional number such as 18.5 or
3.141592653589. Floating point numbers may be specified using either decimal or scientific
notation.
• string: A string is a sequence of characters, like “hello world” or “abracadabra”. String values
may be enclosed in either double quotes (“ and ”) or single quotes(‘ and ’). A blank space inside
the quotes is also considered a character.
• boolean: A Boolean variable simply specifies a true or false value.
• array: An array is a collection of items that have something in common. A detailed discussion is
given in a later lecture.

Variables in PHP are represented by a dollar sign ($) followed by the name of the variable. For
example, $popeye, $one and $INCOME are all valid PHP variable names. The variable name is
case-sensitive.

To assign a value to a variable, you use the assignment operator, the “=” sign. For example, review
the following PHP code:

<?php
$myvar = “Jennifer”;
$Myvar = “Lopez”;
echo “$Myvar, $myvar”;
?>

The output will be:
Lopez, Jennifer

Both $myvar and $Myvar represent two different variables. The “=” sign is the assignment sign.
$myvar is assigned a value “Jennifer”, while $Myvar is assigned another value “Lopez”.

The echo command displays both values to the screen. The value being assigned need not always be fixed; it
could also be another variable, an expression, or even an expression involving other variables.

For example:

<?php
$dog_age=10;
$human_age = $dog_age * 7;
echo “Your dog is $human_age years old in human age.”;
?>

A variable $dog_age is declared and assigned a value 10. The value of $dog_age variable is then
multiplied by 7, giving a value of 70, as the value of the $human_age variable.

The echo command display the value of $human_age, and the output will be:

Your dog is 70 years old in human age.

Interestingly enough, you can also perform more than one assignment at a time

. Consider the following example, which simultaneously assigns three variables to the same value:

<?php
$price1 = $price2 = $price = 15;
?>

Boolean

Boolean variables are commonly used for decision making. For example:

<?php
if ($add_line == TRUE) {
echo “<hr>”;
}
?>

Note: The above example uses if..then statement for decision making. A detailed discussion about
how to use decisive logic is given in later lecture.

Integers

In PHP, integers can be based in decimal (10-based), hexadecimal (16-based) ,or octal (8-based)
notation, optionally preceded by a sign (- or +).

Important concept:

• Decimal numbers have numerals 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9.
• Octal numbers have numerals 0, 1, 2, 3, 4, 5, 6, and 7.
• Hexadecimal numbers have numerals 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, and F.

To define the base of a number, use the following rules:

• if the default base is decimal, there is no need to precede the number with anything.
• to use the octal notation, you must precede the number with a 0 (zero).
• to use hexadecimal notation precede the number with 0x (zeroX).

To specify a positive or negative value, use the following rules:

• numbers are positive by default.
• to specify the negative value, precede the number with a negative “-” sign.
For example,

<?php
$myNo1 = 5678; // decimal number
$myNo2 = -5678; // a negative number
$myNo3 = 0123; // octal number (equivalent to 83 decimal)
$myNo4 = 0x1A; // hexadecimal number (equivalent to 26 decimal)
echo “$myNo1, $myNo2, $myNo3, $myNo4”;
?>

FREE php course ,Floating point numbers

Floating point numbers (also known as “floats”, “doubles” or “real numbers”) can be specified using
any of the following syntax:
Math format. Simply show the integer and the decimal fractions, such as 3.14125.
Scientific format. In scientific format, the natural logarithm (e) is used to denote the exponent.
For example, 6.25X1015 is expressed as 6.25+e15 (or simply 6.25e15), while 2.74X10-23 is 2.74-
e23. The natural logarithm letter distinction (e) is not case sensitive. You can use “e” or “E” to
express scientific formatted numbers.

 

Consider the following example:

<?php
$a = 3.125;
$b = 1.2e3;
$c = 5.27E-10;
echo “$a <br>”;
echo “$b <br>”;
echo “$c <br>”;
?>

The output should look like this:

3.125
1200
5.27E-10

 

Strings

A string is series of characters. A string literal can be specified in two different ways.

• Single quoted. The easiest way to specify a simple string is to enclose it in single quotes (the
character ‘). But, if you use single quotes (‘) for your string, all characters are printed as-is.
• Double quoted. In PHP, escape sequences only work with double quotes (“).
Consider the following example with double quotes,

<?php
echo “Line 1\nLine 2”;
?>

The output should be:

Line 1
Line 2

However, when using single quotes,

<?php
echo ‘Line 1\nLine 2’;
?>

the output should change to (however, the \n is ignored by web browser):

Line 1\nLine 2

The most important difference between the two is that a string enclosed in double quotes is parsed
by PHP.

This means that any variable in it will be expanded.

<?php
$var = ‘apple<br>’;
echo “$var”; // Results in the value of $var being printed
echo ‘$var’; // Results in the word ‘$var’
?>

The output is then:

apple
$var

PHP Variable

There are a few rules that you need to follow when choosing a name for your PHP variables.
• PHP variables must start with a letter – an alpha charater.

Naming Conventions

• PHP variables may only be comprised of alpha-numeric characters and underscores. a-z, A-Z, 0-
9, or _ .
• Variables with more than one word should separate each word with an underscore character
( _ ). For example, $my_variable .
Do not use keywords and reserved words to name variable. They have special meaning in PHP.
Some keywords and reserved words represent functions, constants, or other PHP features.

According to PHP manual (http://us3.php.net/manual/en), $this is a special variable name that
cannot be assigned.

FREE php course , Predefined variables

PHP provides a large number of predefined variables. They are a preferred method for retrieving
external variables. PHP itself creates some of the variables, while many of the other variables change
depending on which operating system and Web server PHP is running. Rather than attempt to
compile a complete listing of available predefined variables, you can run the following script to
display all of the available variables.

<?php
// Show all information, defaults to INFO_ALL
phpinfo();
?>

The following example will display the filename of the currently executing script, the type of web
browser or user agent that is being used to access the current script, the server’s IP address, the
server’s host name, and so on.

<?php
echo $_SERVER[‘PHP_SELF’] . “<br>”;
echo $_SERVER[‘HTTP_USER_AGENT’] . “<br>”;
echo $_SERVER[‘REMOTE_ADDR’] . “<br>”;
echo $_SERVER[‘REQUEST_METHOD’] . “<br>”;
echo $_SERVER[‘SERVER_NAME’] . “<br>”;
echo $_SERVER[‘SERVER_SOFTWARE’] . “<br>”;
echo $_SERVER[‘SERVER_PROTOCOL’];
?>

You can use the following common predefined variable to retrieve information.

 

To view a comprehensive list of Web server, environment, and PHP variables offered on your
particular system setup, simply visits the demo site
at:http://us3.php.net/manual/en/reserved.variables.server.php.

 

Review Questions

1. Which escape sequence is equivalent to the [Tab] key?

A. \tab
B. /tab
C. \t
D. /t

2. Given the following code, what is the output?

<?php
$x = $y = 2;
echo ($y);
?>

A. 11
B. 9
C. 8
D. 2

3. Which is a string?

$a = “Hello, world!”;
$b = “1 2/5”;
$c = “02.23.72”;

A. $a
B. $b
C. $c

D. All of the above

4. Which is a floating-point variable?

$a = 12;
$b = 972;
$c = 15.48;
$d = -43;

A. $a
B. $b

C. $c
D. $d

5. In the following code, $larger is a __ variable.

<?php $larger = TRUE; ?>

A. Comparison
B. Boolean
C. Floating-point
D. Logical

6. The output of the following code is __.

<?php echo 0x1A; ?>

A. 25
B. 26
C. 81
D. 83

7. The output of the following code is __.

<?php
$x = 5;
$z = $y = $x;
echo $z;
?>

A. 5
B. $y
C. $y = $x
D. an error message

8. The output of the following code is __.

<?php
$y = ($x = 7);
echo $y;
?>

A. ($x = 7)
B. 7
C. $x = 7
D. $x

9. Which is a floating-point variable?

<?php
$a = 3.125;
$b = 1.2e3;
$c = 5.27E-10;
?>

A. $a
B. $b
C. $c
D. All of the above

10. Which is a possible output of the following code?

<?php echo $_SERVER[‘SERVER_NAME’]; ?>

A. CISDEpt.com
B. 207.125.11.110
C. SNMP
D. /usr/cis246/test.php

FIND ALL COURSE LECTURES ON THIS LINK 

Happy
Happy
0 %
Sad
Sad
0 %
Excited
Excited
0 %
Sleepy
Sleepy
0 %
Angry
Angry
0 %
Surprise
Surprise
0 %

The post free PHP programming course E2 learn to code appeared first on Cyber Hyena.

]]>
https://cyberhyena.net/blog/2021/03/12/free-php-programming-course-e2-learn-to-code/feed/ 0
free PHP programming course E1 learn to code https://cyberhyena.net/blog/2021/03/12/php-programming-course-e1-learn-to-code/ https://cyberhyena.net/blog/2021/03/12/php-programming-course-e1-learn-to-code/#respond Fri, 12 Mar 2021 19:46:24 +0000 https://cyberhyena.net/?p=194 Introduction to PHP PHP is a widely-used Open Source, general-purpose ,scripting language that is especially suited for Web development and can be embedded into HTML. According to the php.net site, the acronym “PHP” is short for “PHP: Hypertext Preprocessor”. Its syntax draws upon C, Java, and Perl, and is easy to learn. The main goal […]

The post free PHP programming course E1 learn to code appeared first on Cyber Hyena.

]]>
0 0
Read Time:9 Minute, 35 Second

Introduction to PHP

PHP is a widely-used Open Source, general-purpose ,scripting language that is especially suited for Web development and can be embedded into HTML.

According to the php.net site, the acronym “PHP” is short for “PHP: Hypertext Preprocessor”. Its syntax draws upon C, Java, and Perl, and is
easy to learn.

The main goal of the language is to allow web developers to write dynamically generated web pages quickly, but you can do much more with PHP.

You can learn this language by understanding how its primary functions work, how it interacts with HTML, and how its nature of being a server-side scripting language differs from a client-sidelanguage, such as JavaScript.

PHP code must be executed on the server. Only the output it generates is delivered to client browser. Consider the following example.

A PHP script similar to the one above will not run on a client-side browser. It must be execute on a
Web server with the support of a PHP interpreter. The server, after execution, generates the string
output, as shown below, and sends it along with the rest of HTML tags to the client browser.

What the client receives is a completed HTML code looks like:

PHP supports all major operating systems, including Linux/ Unix, Microsoft Windows, Mac OS X,
and others. PHP has also support most of the web servers including Apache, Microsoft Internet
Information Server, Personal Web Server, Netscape, iPlanet servers, and many others.
Basically a web server, such as Apache, needs only to activate support for PHP and that all files
ending in .php are recognized as PHP code. To support a PHP-based data-driven site, you will most
likely want to create and use a database with database languages such as MySQL.
The php.net site

The “http://www.php.net” site is the primary web site for PHP language and support. This site
is where you can download the latest version of PHP along with manuals and other reference
documents. As of September 2010, the latest version is PHP 5.3.3. For Windows users, be sure to
download the Windows Binaries at “http://windows.php.net/download/”.

This site has a search engine. You can consider using it as a reference library. For example, to
search for date-related function, enter the keyword “date” in the textbox.

How does a PHP script look like?

PHP scripts are text files that can be created by any text editor, such as Notepad. It frequently
includes HTML tags. But instead of the .htm or .html extension, the files are given the .php
extension. Within the file, PHP code is contained in PHP tags.

Consider at a very simple PHP script that displays “Welcome to CIS246!” in the browser window.
The script is a text-only file, called example1.php, and the file is uploaded to the server.

This example is a simple but functional PHP script. It uses only an “echo” command to output a
string. Of course, PHP scripts used incommercial web sites will never be so simple. However, this
code gives you a chance to understand how PHP scripts work on the server side.

The PHP-enabled server reads the following HTML embedded PHP code and then executes the
“echo” command.

 

<?php
echo “Welcome to CIS246!”;
?>

The generated string “Welcome to CIS246!“ is then inserted between HTMLand
tags, as shown below, and then only the following HTML code is delivered to the client
browser.

 

<html>
<body>
Welcome to CIS246!
</body>
</html>

 

All of the PHP codes are processed and stripped from the page. The only thing returned to the client
from the Web server is pure HTML output. The source code of the above example tells us the
following:
• Because the page is rendered on the server before it hits the browser, the browser does not see
the PHP code.
• The entire <?php ?> tag is replaced by the HTML output from the PHP code.
• Every PHP statement ends with a semi-colon, “;”

Static vs.
dynamic
PHP code

PHP is an excellent tool for writing dynamic web pages. Non-technical users can easily learn a few
handy tricks to make their web pages easier to manage and more useful.

Technically speaking, all pages that contain PHP code will end in the extension .php. There are
basically two ways to create a PHP page. All PHP pages are variations on these two methods:

Method 1: Static with PHP Blurbs (HTML with embedded PHP code)

<HTML>
<BODY>
<?php
SOME PHP CODE HERE!
?>
</BODY>
</HTML>

Method 2: Dynamic by Default (PHP code with embedded HTML generated code)

<?php
PHP CODE
?>

There are some cases where you MUST use method 2, but for most of our work, you will be able to
use whichever method you prefer.

Beginning and Ending a Block of PHP  Statements

When writing PHP, you need to inform the PHP engine that you want it to execute your PHP
commands. If you don’t do this, the code you write will be mistaken for HTML code and will be
output to the browser. You can do this with special tags such as, <?php … ?>, <? … ?>, or
<script>…</script> that mark the beginning and end of all PHP code blocks.

FREE php course
FREE php course

For example, the following three lines yield the same results.

PHP uses semicolon “;” as the statement delimiter.

Two basic output functions

The two basic PHP output functions are print and echo. These functions are used as follows:

Now, you might be wondering when to use each of these two functions if they’re are similar. Well,
there are really two differences.

Print will place a “newline character” (\n) at the end of each print statement so that you don’t
manually have to print out a new line. With echo, you really should use:

echo “Hello World\n”;

The \n is the representation of a newline. The PHP echo function can also accept multiple
arguments, as in:

echo “Hello “, “World!”, “\n”;

Commenting your PHP code

You can use either a single quote (‘) or a double quote (“) unless what you are trying to print
contains a variable or a special character (like \n). More details on that in later lecture.

Commenting your code is a good habit to practice. Entering comments in HTML documents helps
you (and others who might have to edit your document later) keep track of what’s going on in large
amounts of code. Comments also allow you to write notes to yourself during the development
process, or to comment out parts of code when you are testing your scripts, in order to keep code
from being executed.

HTML comments are ignored by the browser and are contained within <!– and –> tags. For
example, the following comment reminds you that the next bit of HTML code contains a logo
graphic:

<!– logo graphic goes here –>

PHP uses comments, too, which are ignored by the PHP parser (interpreter). PHP comments are
usually preceded by a double slash, like this:

// this is a comment in PHP code

However, you can also use the following type of comment syntax for multiple-line comments:

/* Put a multiple-line comment here */
# using Linux/Unix shell type of comment

Formatting PHP outputs using HTML tags    FREE php course

Essentially, PHP is an inline scripting language developed for use with HTML. What this means is
that PHP and HTML can be intertwined with each other in a webpage. This means that not all of the
webpage must be generated by the PHP. You can use HTML formatting tags to format output
generated by PHP. For example,

The output will be:

You can also include the formatting HTML tags inside the double quotes of the PHP echo
command. For example,

 

Installing PHP  FREE php course

The Windows PHP installer is built using Microsoft’s MSI technology, so its file name has an
extension of .msi (e.g.: php-5.3.0-nts-Win32-VC9-x86.msi). After downloading the PHP
installer, run the MSI installer and follow the instructions provided by the installation wizard. It will
install and configure PHP. There is a “Manual Installation Steps” guide that will help manually
install and configure PHP with a web server installed in a Microsoft Windows operating system. It
is available at http://www.php.net/manual/en/install.windows.manual.php.

For Mac OS X users, there are notes and hints specific to installing PHP on Mac OS X. Details are
available at http://www.php.net/manual/en/install.macosx.php.
For Linux users, simply refer to the manual provided by the distributer. For Fedora users, use the
following to install Apache (httpd), PHP, MySQL (server and client), and the component that allows
PHP to talk to MySQL using the root account.

 

yum -y install httpd php mysql mysql-server php-mysql

questions FREE php course

1. What does PHP stand for?
A. Private Home Page
B. Personal Home Page
C. PHP: Hypertext Preprocessor
D. Personal Hypertext Processor
2. PHP server scripts are surrounded by which delimiters?
A. <?php>…</?>
B. <?php…?>
C. <script>…</script>
D. <&>…</&>
3. How do you write “Hello World” in PHP?
A. echo “Hello World”;
B. Document.Write(“Hello World”);
C. “Hello World”;
D. Console.Write(“Hello world”);
4. All variables in PHP start with which symbol?
A. $
B. !
C. &
D. #
5. What is the correct way to end a PHP statement?
A. .
B. ;
C. New line
D. </php>
6. What is a correct way to add a comment in PHP?
A. *\..\*
B. /*…*/
C. <!–…–>
D. <comment>…</comment>
7. Which is the primary Web site for PHP language and supports?
A. http://www.php.com
B. http://www.php.gov
C. http://www.php.net
D. http://www.php.org
8. Which PHP code can yield the following output from a PHP file?
Welcome to CIS246!
A. <?php echo <i>”Welcome to CIS246!”</i>; ?>
B. <?php <i>”Welcome to CIS246!”</i>; ?>
C. <?php echo_i(“Welcome to CIS246!”); ?>
D. <i><?php echo “Welcome to CIS246!”; ?></i>
9. What does the client browser receive after the PHP server executes the following PHP script?
<html></body><?php echo “CIS246!”; ?></body></html>
A. <html></body><?php echo “CIS246!”; ?></body></html>
B. <html></body>echo “CIS246!”; </body></html>
C. <html></body>”CIS246!”</body></html>
D. <html></body>CIS246!</body></html>
10. Which of the following would be a Windows PHP installer for Windows operating system?
A. php-5.3.0-nts-Win32-VC9-x86.ins
B. php-5.3.0-nts-Win32-VC9-x86.com
C. php-5.3.0-nts-Win32-VC9-x86.php
D. php-5.3.0-nts-Win32-VC9-x86.msi
FREE php course

for full lists of episodes click here 

Happy
Happy
0 %
Sad
Sad
0 %
Excited
Excited
0 %
Sleepy
Sleepy
0 %
Angry
Angry
0 %
Surprise
Surprise
0 %

The post free PHP programming course E1 learn to code appeared first on Cyber Hyena.

]]>
https://cyberhyena.net/blog/2021/03/12/php-programming-course-e1-learn-to-code/feed/ 0