Learn PHP in One Day and Learn It
Well for Beginner and its freee

In This Blog we’ll be covering most of the major topics in
PHP. These topics are carefully chosen to give you a broad exposure to PHP
while not overwhelming you with unnecessary details.
Before we dive into PHP proper, note that this book requires
you to have a basic understanding of HTML and MySQL.
What is PHP?
PHP is a general-purpose programming language used mostly
for web development. Created by Rasmus Lerdorf in 1994, it allows developers to
create dynamic web pages with ease. For instance, developers can create a form
in HTML and process it using PHP. Depending on the inputs entered into the
form, developers can use PHP to display different outputs to users. Most of the
time, PHP is used as a server-side language. This means that PHP code is not
processed on the user’s computer (also known as a client). In other words, when
you access a PHP page on your browser, the code is not processed on your
computer. Instead, your browser sends a request to a web server, which then
processes the code and returns the result to the browser in the form of a web
page. More often than not, this web server is a remote computer where the PHP
files are stored. For the web server to process PHP code, a special software
known as the PHP interpreter needs to be installed. We’ll learn to set up our
web server and install the PHP interpreter using a free software called XAMPP.
Why Learn PHP?
There are many reasons for learning PHP.
Firstly, PHP is one of the most widely used web programming
languages and is used in many popular content management systems such as
Wordpress, Drupal and Joomla.
As such, the demand for PHP programmers is high. If you plan
on working as a freelance developer, PHP is an essential skill to have. Next,
PHP is designed to be beginner-friendly and easy to learn. In addition, due to
the popularity of the language, if you run into any issues with your PHP code,
you can find help easily. A simple search on the internet will likely help you
resolve most of the problems you face.
Last but not least, the syntax of PHP is very similar to
other programming languages such as Java or C. Once you are familiar with PHP,
you’ll find it much easier to master other languages. Ready to get started?
Let’s do it!
Installing XAMPP
we’ll learn to set up our web server and install the PHP
interpreter. In addition, as we’ll be using PHP to interact with a database
later, we need to set up our database server too.
To set up our web and database servers, we need to install
three software: the Apache web server, the PHP interpreter and the MarieDB
database server.
Installing all three software can be tedious if we do them
one by one. Fortunately, some kind folks at Apache Friends created a free
package that contains all the software we need. This package is known as XAMPP,
which stands for Cross-platform (X), Apache web server (A), MarieDB database
server (M), PHP (P) and Perl (P).
The MarieDB database server is a community-developed fork of
the MySQL server. Although M in XAMPP officially stands for MarieDB, you’ll see
that XAMPP labels the database server as MySQL in the software. We’ll follow
this label and refer to the database server as MySQL.
For detailed instructions on installing XAMPP and using it
to set up our servers, check out servers, check out https://learncodingfast.com/how-to-install-xampp-and-brackets
Instructions are available on the accompanying site of this
book so that whenever there are any changes to XAMPP, you can find the updated
instructions on the site. This will ensure that you’ll always get the latest
installation instructions.
Besides installing XAMPP, you are strongly encouraged to
install an advanced text editor to do your coding in. While you can definitely
code in a basic text editor (such as Notepad), an advanced text editor offers
features like syntax highlighting that makes your code easier to read and
debug.
A recommended free editor is Brackets. For instructions on
installing Brackets, head over to https://learncodingfast.com/how-to-install-xampp-and-brackets
as well.
Configuring php.ini
After you have installed XAMPP and Brackets on your
computer, you are ready to start creating your dynamic website.
However, before we do that, we need to make some changes to
the error settings on our PHP server. This ensures that whenever there’s any
mistake in our code, an error message will be displayed on the browser.
To configure these settings, the most direct way is to
modify a file called php.ini. The instructions below are valid at the time of
writing. If there are any changes to the instructions, you can find the updated
instructions at https://learncodingfast.com/php
.
To modify php.ini, we need to locate the file first.
To do that on Windows, launch XAMPP and click on the
“Config“ button for Apache. Next, select php.ini. The file will be opened in
Notepad.
On Mac, launch XAMPP and click on the “Start” button in the
“General” tab.
Wait for the status to turn green. Next, select the
“Volumes” tab and click on the “Mount” button. Finally, click on “Explore” and
look for the “etc” folder.
You’ll find php.ini inside this folder. Open it using
Brackets.
Once php.ini is open, scroll to the bottom of the page and
add the following lines to it:
Error_reporting=E ALL
Display_error=on
These lines are added to the bottom of the page so that if
the settings have been set previously (in earlier parts of the php.ini file), the new lines will
override the previous settings.
Next, save the file and restart Apache. On Windows, just
stop the Apache server (if it is currently running) and start it again. On Mac,
select the “Services” tab and click on “Apache”. If Apache is currently
running, click on the “Restart” button. Else, click on the “Start” button. Wait
for Apache to restart and your settings will be updated.
You’ll now get an error message whenever there’s an error in
your code. Depending on the PHP version you are using, some of the errors you
encounter when testing the code
Important Links
Before proceeding to code our first web page, I would like
to bring your attention to two important links. The first is https://learncodingfast.com/php .
This book uses lots of examples to illustrate different
concepts. While you are encouraged to type out these examples in Brackets
yourself, if you prefer not to, you can download the source code at the link
above. The project files and any additional notes or updates to the book can
also be found there.
Next, we have the errata page. While every effort has been
made to ensure that there are no errors in the book and all source code has
been tested extensively on multiple machines, if there are any errors we
missed, you can find the errata at https://learncodingfast.com/errata
.
Coding our first Web Page
Great! We are now ready to start coding. First, ensure that
you have started Apache in XAMPP.
Note: If you have
any problems starting Apache, it is likely due to a port conflict. Follow the
instructions at https://learncodingfast.com/how-to-install-xampp-and-brackets
to resolve the conflict.
Next, create a new file in Brackets and add the following
code to it:
<!DOCTYPE html>
<html>
<head>
<title>My first PHP page</title>
</head>
<body>
<h1>My first PHP page</h1>
<?php
#Simple hello world page
echo "Hello World!";
?>
</body>
</html>
If you are using an ebook reader, some lines above may
wrap to the next line due to the limited width of the reader. If that occurs,
try changing your device to the landscape mode or using a smaller font size.
The same applies to all the other examples in this book.
Save the file above as hello.php to your htdocs
folder. Any file that contains PHP code must be saved with a .php extension. A
file that ends with a .php extension is also known as a PHP script.
You can find the htdocs
folder inside the XAMPP (Windows) or LAMPP (Mac) folder.
htdocs is
the root directory of your local web server. On Windows, to load the files
stored in htdocs, you simply type http://localhost/<filename> into
your browser’s address bar. For instance, to load hello.php, type http://locahost/hello.php.
On Mac, the easiest way to load files stored in htdocs is to enable port forwarding. This
can be done under the “Network” tab in XAMPP. Suppose you have enabled
“localhost:8080->80(Over SSH)” in XAMPP, to load hello.php, type http://locahost:8080/hello.php into your browser’s address bar.
Got it?
You can refer to https://learncodingfast.com/how-to-installxampp-and-brackets for more
detailed instructions.
For ease of reference, from this point forward, I
will use http://localhost
to refer to your localhost. If you are using Mac, remember to add
the port number to the URL (i.e., http://localhost:8080).
Let’s analyze the code in hello.php now. Most of the code should look familiar to you except
the following:
<?php
#Simple hello world page
echo "Hello World!";
?>
The <?php … ?> tags above are
known as PHP tags; they tell the server that code enclosed within should be
treated as PHP code.
In our example, we have two simple lines of PHP code.
The first line
#Simple hello world page
is known as a comment. Comments are written by
programmers to explain their code to other programmers; these comments are
ignored by the PHP interpreter.
To add a single line comment to our code, we precede
the comment with # or //.
For instance,
# This is a comment
// This is also a comment
To add multiple lines comment, we enclose the
comment in /*...*/.
For instance,
/*This is an example of a multi-line comment. */
After the comment, we use the statement
echo "Hello World!";
to display the words “Hello World!” on the browser.
This is known as an echo statement; we’ll learn more about it in the next
chapter.
Notice that we end the echo statement with a semicolon (;)?
All statements in PHP must end with a semicolon. This is similar to how we end
sentences with a period (.) in English.
If you load hello.php
now, you’ll get

as the output.
That’s it. We have just coded our first PHP web page.
Simple? Great!
Let’s move on.
Basic PHP Tasks
In the previous chapter, we installed XAMPP and
Brackets and coded our first PHP script.
In this chapter, we’re going to learn to perform
three very fundamental tasks in PHP - displaying outputs to users, duplicating
code across multiple pages and redirecting users.
Displaying Outputs
Before we learn to display outputs, let’s create a
PHP file to serve as a template for us to test the PHP statements in this
section.
Create a file in Brackets and save it as output.php to your htdocs folder. Add the following code to output.php:
<?php
//Add code here
Whenever you want to test any PHP statement in this
section, you can add the statement to output.php
(after the <?php
opening tag) and load it in your browser to check the output.
Notice that we did not add a closing tag (?>)
to output.php? This is because output.php only contains PHP code.
According to the standard recommended by the PHP
Framework Interop Group, any PHP script that only
contains PHP code should not end with a closing tag. This is to prevent
unwanted whitespaces at the end of the file, which can cause issues such as an
“Headers already sent” warning when modifying response headers.
Do not worry if the statement above does not make
any sense; we’ll learn about the “Headers already sent” warning later. For now,
let’s move on to learn how to display outputs in PHP.
echo
The easiest way to display outputs in PHP is to use
an echo
statement. To do that, we enclose the text that we want to display in a
pair of matching quotation marks after the echo keyword. The echo keyword
is not case sensitive. Hence, echo, Echo or ECHO
will all work.
Using an echo statement is
straightforward. We did that in Chapter 2 when we used it to display the words
“Hello World!”.
In addition to using echo statements to display simple
text messages, we can use them to display text with HTML markup. For instance,
we can write
echo 'PHP is fun<BR>and easy!';
This gives us
PHP is fun and easy!
as the output; the text “and easy!” is displayed on
the second line due to the <BR> tag.
We can also combine multiple messages in a single echo
statement. To do that, we separate the messages with commas. For instance,
echo "ABCD", "EFGH";
gives us
ABCDEFGH
as the output. Combining messages is useful when our
message is more complex, such as when the message includes variables and
functions. We’ll learn about variables and functions in later parts of the
book.
Last but not least, we can use an echo
statement with or without parentheses. You may have seen some code that does
the following:
echo ("ABCD");
This is fine in most cases. However, if you use
parentheses, you are not allowed to combine multiple messages in a single echo
statement.
For instance,
echo ("ABCD", "EFGH");
will give us an error. In most cases, you are
better off using echo statements without parentheses.
print
Besides using echo statements to display
outputs, we can use print statements.
print statements are VERY similar to echo
statements. Let’s look at an example:
print "My name is Brody."; gives
us
My name is Brody.
as the output. As you can see, the print
statement works just like an echo statement. While there are some differences between
them, in most cases, there is no need to use the print statement.
In PHP, it is fairly common that there is more than
one way to perform a certain task. This is one of the main complaints
developers have about PHP, as it complicates things unnecessarily. In most
cases, the different ways are very similar to each other. In order not to
overwhelm you with too many options, I'll stick with one option in this book
and briefly mention other options if necessary. In this book, I’ll be using echo
statements to display outputs.
also read
https://u2suggestions.blogspot.com/2022/10/how-to-build-website-with-html-complete.html
back to topic
Escaping Characters
Next, let’s learn to escape characters in PHP.
In the previous sections, we learned to enclose text
in quotation marks when we use an echo (or print) statement.
What happens if we want to display an apostrophe?
For instance, suppose we have the following
statement:
echo '<BR>Today is Friday, we're going to the zoo.';
We’ll get an error when we try to run the statement
above as PHP does not differentiate between an apostrophe and a quotation mark
by default. Hence, it treats the apostrophe in the word “we’re” as a closing
quotation mark. In other words, it sees the statement as
echo '<BR>Today is Friday, we'
and expects a semicolon or comma after the
apostrophe. When that does not happen, PHP gets confused and gives us an error.
There are two
ways to deal with the error above. The first is to use double quotation marks
to enclose the text and rewrite the statement as echo "<BR>Today is Friday,
we're going to the zoo.";
As PHP uses a double quotation mark to close another
double quotation mark, the apostrophe in “we’re” is not mistakenly interpreted
as a closing quotation mark.
Alternatively, we can escape the apostrophe in the
original statement by preceding it with a backslash character (\).
Escaping a character alters the meaning of the
character. Without the backslash character, the apostrophe is interpreted as a
closing single quote. With it, it is treated as an apostrophe meant to be
displayed on the browser.
For instance, to display the apostrophe in the
previous example, we can use the statement below:
echo '<BR>Today is Friday, we\'re going to the zoo.';
This gives us the following output:
Today is Friday, we're going to the zoo.
Other common uses of the backslash character include:
Using
it to display a double quotation mark
echo """;
gives us an error, while
echo "\""; gives
us
"
Using it to display the backslash character itself
echo "\";
gives us an error, while
echo "\\"; gives us
\
I think you bore so I am provide you pdf
Click " here"

0 Comments