SEC S20W4|| Introduction to PHP Part -2

in dynamicdevs-s20w4 •  last month 

Hi friends, after reading around I am interested in participating in an amazing challenge this week. The challenge is titled: Introduction to PHP Part -2 by Mr. @kouba01 and Mr. @starrchris following the contest link: SEC S20W4|| Introduction to PHP Part -2

Previously I also invited other friends such as Mr. @irawandedy, Mr @muzaack1 , sir @sisol, sir @aneukpineung78 and my friend @cymolan.

burnsteem25.jpg

I edited with Canva

Part 1: Theoretical Questions

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

After reading through some PHP websites, I found that the definition of Exception in PHP is a way that is created with the intention of handling errors in PHP code. Exceptions allow us to catch errors and handle them in a more supervised manner. Usually, exceptions are used as a form of error handling, and these errors are usually unexpected and will redirect the program flow to the fast lane if an error does occur.

In PHP, people generally utilize the try-catch block to catch exceptions. In the try block, of course, we will provide code that usually raises an exception, while in the catch block, of course, we will write code with the intention of handling the exception.

A simple example is as follows:
I use Visual Studio Code as a place to edit code:
1.jpg
A little explanation in the code above is :

  • The try block: In this code I write to make sure what might cause an error. In the example above, I tried to divide the number 12 by the divisor value that I set to 0. As we want to prove that division by zero is not allowed, I will see if the divisor value before performing the operation.
  • The checking block (if): But first I will check and make sure whether the value of the divisor is zero or not. if it is, then I will throw an exception by throwing new Exception(), which will trigger the catch block.
  • Catch block: in this code I will catch the exception and to display the appropriate error message that I want. Normally this exception will be triggered when a division by zero condition is true.
  • End result: So if the divisor is real, which is zero, the system will display the message “Sorry an error occurred: Error: Sorry can't divide by zero.” Thus the code will not crash, and we know what the error is.

To run the php coding above, the next step is to turn on XAMPP, with my goal is to run the results on the web like Google Chrome or Mozilla or so on.

image.png

Next I went to the web and opened http://localhost/... (PHP folder and file name). And this is the result:

image.png

1.2 Six Types of Errors: Describe six common types of errors in PHP, explaining each with a concrete example.
  • Parse Error: This may have happened often, especially to a student like me. Usually this error occurs when there is a syntax error, a simple example is the lack of parentheses or semicolons because we often use it as a closing. The result of this error is that PHP cannot run the code.
    2.jpg
    Then it will usually show Error: Parse error: syntax error. The cause lies in the closing quotation marks that are not given in the coding above.
  • Warning: This kind of error doesn't stop the script, but PHP's habit is to give an important warning, for example about a file that may have been forgotten or not included.
    3.jpg
    Then it will usually display the message “Error: Warning: include(): failed to open stream”. And the reason is because the file really wasn't found.
  • Fatal Error: This kind of error in PHP will stop completely because the code is trying to do something impossible, a small example is when calling a function that does not exist.
    4.jpg
    Typically the error will display the message “Error: Fatal error: Call to undefined function”, of course the cause will be that the function you want to call does not exist.
  • Notice: This kind of error lies in PHP when it wants to notify something unusual, a simple example is when we use a variable that may not have been declared, but this php code will still run.
    5.jpg
    This kind of error will display the message “Error: Notice: Undefined variable”. The cause is obvious because the variable has not been defined.
  • Strict Error: The occurrence of this error in PHP code is when the code violates a strict rule, a simple example is when the method used does not match between the main class and its children.
    6.jpg
    The message displayed is “Error: Strict Standards: Declaration should be compatible”. The cause is that the method signature does not match.
  • Deprecated Error: This error has happened very, very often to myself, while this error usually occurs when we use features that are deprecated and no longer recommended.
    7.jpg
    The message that will be displayed is “Error: Deprecated: mysql_connect() is deprecated”. The cause is that the mysql_connect() function is deprecated or obsolete.
1.3 Errors and Personal Images: Describe what errors exist in programming, with three personal screenshots of the errors you encountered, then explain each one.

8.jpg
In the above coding there are 3 errors that make the error display as below:
image.png
Here's a little explanation from me about that error:
The first is a Parse Error that we may have explained before, well In the first line, there is an echo command that is wrong because there is no semicolon at the end (;). So that this kind of error will This will bring up Parse Error when we run the program, the way I overcome it is usually by paying close attention to the notification and then looking for solutions in the videos on youtube so that the solution can be found.

Furthermore, there is another Fatal Error contained in the second line, please note that we actually want to try to call the undefined_function() function which is not defined. As a result, it raises a Fatal Error because the PHP that we run has not found the function.

The last one is the Warning error, now we can see on the third line, that PHP wants to include the walictd.php file, which clearly does not exist or we have not created it. So it will produce the Warning error, but the next code (echo command) can still run.

1.4 Includes and its Importance: Explain includesyntax in PHP and why it is important to reuse code, give examples.

Please note that include and require are utilized in order to include other PHP files in the file being worked on. So we can assume that include is very important so that it can separate the code into many parts, such as linking the footer file to the header or vice versa, or the configuration file to the original script.

9.jpg
In the example above, we want to display Footer and Header on the “welcome it” web, then by utilizing include, we can call back the header.php and footer.php files on many other pages without rewriting the code which only makes us longer and longer code.

1.5 Koneksi Basis Data: Jelaskan langkah-langkah untuk menghubungkan basis data ke file PHP, dengan contoh kode (variabel koneksi, mysqli_connect()atau PDO).

Here are the steps when I want to connect the database to PHP using either mysqli or PDO:

The first when I use mysqli_connect:
suppose I have previously created a database with the name walictd.db
image.png
Next, I only create a connection php file for the purpose of connecting between the database and php. Here's an example of the php connection code that I created:
10.jpg

Then I will test it on the web, whether the code runs smoothly so that the database is successfully connected or not and the following results:

image.png

Part 2: Practice Questions

2.1 Pages with includes: Create a PHP page ( learn.php) and use the includesintaxis to import the header, navigation bar, sidebar, and footer.

In this section I will make a more structured version and I like the more elegant appearance of the PHP learn.php page, of course, do not forget to utilize the include with the aim of including headers, navbars, sidebars, and footers. Hopefully my design can provide a more attractive appearance and friends all like it.

First of all, I will create 5 PHP files to form an attractive appearance. The 5 PHP files are: header.php, navbar.php, sidebar.php, footer.php and learn.php, not that I might add css so that the display can be more interesting. I edit all these files using Visual Studio Code:

Before entering the coding, I first created a folder called “learn” and stored it in (C:\xampp\htdocs\learn). Inside the folder I will fill it with the learn.php file, and create a new folder as well which I name “includes” and “assets”. In the “includes” folder I will fill it with files: header.php, navbar.php, sidebar.php, footer.php, while in the “assets” folder I will fill it with: style.css.

Here's the coding:

  1. header.php
    a.jpg

  2. navbar.php
    b.jpg

  3. sidebar.php
    c.jpg

  4. footer.php
    d.jpg

  5. learn.php
    e.jpg

  6. style.css
    f.jpg

After making sure all the files above are saved according to what I have said above, or we can see the order as below:
g.jpg

Next, we just need to start XAMPP, then run the program using CHROME. We simply type “http://localhost/learn/learn.php”, then it will look like this:
image.png

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

To create a database file, we must first make sure to start XAMPP, where we must activate Apache and Mysql.
1a.jpg

Then we type “http://localhost/phpmyadmin/” in Chrome, then it will definitely be displayed as follows:
1b.jpg
Select 1 to create a new database, fill in the database name in column number 2 and save it in number 3. In the picture above I gave the database name is walictd. Next, I will create 4 tables for the registration page, then I will press the SQL menu on the walictd database:
1c.jpg
Actually, you can create the table manually one by one, but above I made it faster with coding like number 2, then I just chose number 3 to start creating the table. To make it faster, I can create data by giving the command code in the SQL menu as follows:
1c.jpg

Above we created a table called “user” with the intention of storing registration data. Here's how it looks to open the user table:
1d.jpg
To open and view the user table, we simply select the user as column 1 above, then select the structure in column 2 and the contents of table 3 will be displayed where there are id, username, email, and password.

2.3 Enrollment Page: Create a PHP registration page to store the data of five students in the database.

The next step is that we will frame the registration form which in the form will contain name, age, address, email, and password data, after which we will store all the data in the users table that we created earlier.

The steps to assemble a page containing the Registration form are as follows;

  • Frame a file that we name register.php.
    Here's the coding I made:
    2a.jpg
  • Next, we create a file that functions to process the registration data and we name the file process_register.php.
    2b.jpg
  • Then we create a file that will connect between php and the database that we have created, we name the file config.php. Here's the code:
    2c.jpg
    To make it more attractive, we will add a color to the background and create a nice element to make it more elegant. When we want to be like that, then we have to create one more css file. Previously in the “C:\xampp\htdocs\project-folder” folder we will add a folder that we name css to store the css file. The css file we give the name “style.css”.
    css.jpg
    Next we will see the main view of the register.php file by typing “http://localhost/project-folder/register.php” in Chrome. Here's what it looks like:
    image.png|image.png
    -|-

Next, we will fill in 5 participant data properly so that it can be directly entered and stored in the database.
image.png

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

Next we will create a login menu. Then the step we do is create a “login.php” file. I provide the code as below:
2d.jpg

In every file there must be a process, so we have to create another file called “process_login.php” and I provide the following code:
2e.jpg

Next I will show the login display menu, I just call the following command: http://localhost/project-folder/login.php, then it looks like below:

image.pngimage.png

After successful login, there will be a display of “Login successful!”
image.png

lv_0_20241004215156.gif

Source :

https://www.php.net/manual/en/language.exceptions.php
https://www.codepolitan.com/blog/beberapa-jenis-error-yang-mungkin-kamu-temui-saat-menggunakan-php/
https://jagongoding.com/web/php/menengah/jenis-dan-penanganan-error/

Best Regard
@walictd
baawah.png

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  
Loading...

Hello @walictd hope you are having a good day awesome post about PHP also took part in this contest, and I loved you explained exceptions and error handling.

Your examples are easy to get. It would be super cool if you could share a video showing your code in action it would help us see how it all works.Wish you luck 🤗🌼🌸.

GIF_20240928_152713_182.gif

Hi, sir. I like the idea of using GIFs to show a demo of your application. I was thinking of making a video, but I just shared pictures. I didn’t think about using GIFs. I invite you to read my blog: Introduction to PHP - Part 2.

Upvoted. Thank You for sending some of your rewards to @null. It will make Steem stronger.

menarik sekaligus memumangkan .. good luck bro ..

itulah yang membuat long pusing dan gak mood lagi wkkwkwk

galak-galak cok peulaken cilet bak pruet nyan keuh moden 😆😆😆


💯⚜2️⃣0️⃣2️⃣4️⃣ This is a manual curation from the @tipu Curation Project

@tipu curate

thankyou sir