SEC S20W4|| Introduction to PHP Part -2

in #dynamicdevs-s20w42 months ago (edited)

1000201350.png

Dear Steemians,

After three weeks of learning and discoveries about databases, SQL language, and PHP, we are pleased to invite you to participate in the SEC S20W4 competition for the fourth week, continuing this exciting journey.

In this fourth part, we will educate you on PHP, another important scripting language that is crucial for your journey as a web developer. We will teach you its exception handlers, errors Include syntax, on how to connect PHP to the database.


Exception handlers

When developing using PHP and other languages (scripting languages and programming languages), the codes for the project could be very cumbersome as a result of the high chance that you might experience some errors when running your codes. These errors could be very dangerous as they can cause your code or project to crash without performing the task it is supposed to run or perform.

This could be very frustrating and depressing because the developer in charge might not know where the error is coming from but with the help of these exception handlers, such problems could be easily settled.

When developing a project it is usually advised that an exception handler should be added to complex tasks that's my involve accepting input from users, that have to do with large file transfers or even network-related activities like API. This is because such job activities can easily generate errors even though the code written is correct. There are two famous types of exception handlers, they are

The Try and catch block:
Has the name implies the try and catch block is a type of exception handler that tries the code and catches any sort of errors that is supposed to crash the code. That is to say, these exception handlers would continuously watch the codes for errors, if there are no errors the codes would go perfectly but if there are errors, they could highlight or notify you of the errors.


try {
// Line of code you want to execute";}
catch (Exception $e) { echo 'Caught exception: ', $e->getMessage();
// This prints an error message
}


As you can see above the code that you tend to execute is written inside a try block while the catch holds just one parameter "Exception $e" and its only job is to print out the errors if any ever comes up from the code. In summary, it helps developers or programmers easily find errors in their code and effectively handle the error.

• Throw:

The throw exception handler is a very similar to that of the try and catch block exception handler. The major difference is that the throw exception can be embedded inside another code unlike the try and catch block. Another important of it is that developers can easily rethrow these errors while they are still developing. This helps improve the efficiency of a developer and their project.


throw new Exception("// error message"); }



Errors

In as much as no developer or programmer ever wishes to generate bugs or errors from their code, the chances of generating errors or bugs from complex and large programs are very high so therefore in order to be very effective and efficient in your duties since necessary you learn about these errors. Another very important reason you need to know about errors is that whenever your exception handler shows you or catches an error, it will be easier for you to understand what causes the error and how to produce effective solutions to resolve those errors or bugs in your code. Below are four types of errors in PHP:


Parse Error (Syntax Error):

This is one of the most common types of error you can encounter while using PHP, this is because it is usually a result of using wrong syntax, such as semicolons, brackets, or poorly structured code.
For Example:
Parse error: syntax error, unexpected '*'

Fatal Error:

As the name implies this errors are encountered when PHP sees a critical error that would prevent the code from completing, such as calling an object, class, or even function that's not in the code. These errors are not to be ignored.

Fatal error: Call to undefined function learn ()

Warning Error:

As the name implies this type of error is a warning error that tells developers or programmers that a specific results in which they are about to get or in search of cannot be seen or reached. Unlike the fatal error, the warning error permit the code to run to completion without any serious interference.

Warning: include(non_existent_Steem.php).

Deprecated Error:

As the name implies these types of errors are usually seen when a developer is trying to use a depreciated code. That is to say, because of the fact that these technologies usually get upgrades there are higher chances that some code becomes depreciated and replaced with new types of codes, so whenever these old codes I used depreciated errors are usually popped up to tell developers that's an upgraded code is out.

Deprecated: Function fly() is deprecated


Include Syntax

The include syntax is an exceptional code that brings joy to every developer's mindset, this is because it's focused on making the development of web pages easier and faster compared to how it was done before. With the help of the include syntax developers don't need to create numerous headers, footers, nav bars, and even side bars for all the pages on their website.

That is to say, this new technology makes it possible for a developer to write code for his header or his specific function and efficiently reuse it on all the pages of their website. This works similarly to that of the function keyword, just that functions help developers use a specific code throughout the program in a page but with include you can be used to set of code in different web pages simultaneously.

This is very important because it helps reduce the line of code, the size of the full project, the processing speed, and the efficiency of the project.

The include syntax

<?php include 'file name.php';?>


Connecting PHP to database

Every proper working web page requires a database to store data and easily retrieve its data, so therefore in this topic we will be discussing how we can efficiently use PHP to create a login page and a sign page which are then linked to a database to store the registered user data and retrieve users for login purpose. Then you check your code to be sure there are no bugs in between, the last code would help print out any sort of error that stops the code from running.

Before we start the first thing you are expected to do is to create a new file usually database.php or something similar, this is because it's inside this file your database code would be created. Then you are expected to add an include syntax in either the day registration page or the sign-up page, this is because it automatically links the login page or register page to the database page.


1000202425.jpg


`
Step 1: We are to create four variables and we are to store four important data which are the server name, the account username on the database, the database password, and the database name, this data and then stored in another variable known as conn or connection. To connect a database to PHP files we must follow three basic steps which are:

// Database connection parameters

$servername = "server name"; // or your server name
$username = "root"; // your database username
$password = ""; // your database password
$dbname = "Database name"; // your database name

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection

if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error); }

step 2:
At this stage, it is expected that our database is already connected to the PHP, so the next step will be sending or better still posting data from the input form to the database

1000202253.jpg

In the above picture you can see that we assigned all the input names to variables that will later be called in the future, that is to say, all data given to the input form would be stored in these variables.

Step3

In the third and final part, we implemented some SQL statements that would aid the sending of this data to the database. After the SQL statements we then close the connection as seen below.

1000202302.jpg



My page output (sign up takes you to login page, if successful, the login then takes me to my site if successfully executed)


1000201212.png

Section 1: Theoretical Questions

-Exceptions: Explain in detail what exceptions are, what they are used for, and how they handle errors in PHP. Include an example of using the try and catch blocks.

-Six Types of Errors: Describe six common types of errors in PHP, explaining each with concrete examples.

-Personal Errors and Images: Explain what errors are in programming, with three personal screenshots of errors you have encountered, and describe each.

-Includes and Importance: Explain the include syntax in PHP and why it is important for reusing code, giving examples.

-Database Connection: Describe the steps to connect a database to a PHP file, with code examples (connection variables, mysqli_connect() or PDO).

Section 2: Practice Questions

-Page with include: Create a PHP page (learn.php) and use the include syntax to import the header, navigation bar, sidebar and footer.

-Database for Registration Page: Create a database capable of storing the data for a registration page.

-Registration Page: Create a PHP registration page to save the data of five students in a database.

-Login Page: Create a PHP login page that verifies the login information with the ones stored in the database and redirects the user if successful.


Contest Guidelines

Post can be written in any community or your own blog.

The post must be #steemexclusive.

Use the following title: SEC S20W4|| Introduction to PHP Part -2

Participants must be verified and active users on the platform.

Post must be more than 350 words. (350 to 500 words)

The images used must be the author's own or free of copyright. (Don't forget to include the source.)

Participants should not use any bot voting services, do not engage in vote buying.

The participation schedule is between Monday, September 30 , 2024 at 00:00 UTC to Sunday, - October 6, 2024 at 23:59 UTC.

Community moderators would leave quality ratings of your articles and likely upvotes.

The publication can be in any language.

Plagiarism and use of AI is prohibited.

Participants must appropriately follow #club5050 or #club75 or #club100.

Use the tags #dynamicdevs-s20w4 , #country (example- #tunisia, #Nigeria) #steemexclusive.

Use the #burnsteem25 tag only if you have set the 25% payee to @null.

Post the link to your entry in the comments section of this contest post. (very important).

Invite at least 3 friends to participate in this contest.

Strive to leave valuable feedback on other people's entries.

Share your post on Twitter and drop the link as a comment on your post.

Your article must get at least 10 upvotes and 5 valid comments to count as valid in the contest, so be sure to interact with other users' entries

Rewards

SC01/SC02 would be checking on the entire 16 participating Teaching Teams and Challengers and upvoting outstanding content. Upvote is not guaranteed for all articles. Kindly take note.

At the end of the week, we would nominate the top 5 users who had performed well in the contest and would be eligible for votes from SC01/SC02.

Important Notice: The selection of the five should be based solely on the quality of their post. Number of comments is no longer a factor to be considered.


Best Regards,
Dynamic Devs Team
@kouba01

Coin Marketplace

STEEM 0.22
TRX 0.20
JST 0.034
BTC 97530.68
ETH 3346.63
USDT 1.00
SBD 3.06