SEC S20W4|| Introduction to PHP Part -2steemCreated with Sketch.

in hive-167213 โ€ขย  last monthย  (edited)

๐ŸŒŸ Enhancing My PHP Development Skills ๐ŸŒŸ

Hey friends! Iโ€™ve come across an exciting challenge that Iโ€™m eager to join this week. Itโ€™s called Introduction to PHP Part - 2, hosted by @starrchris. You can find more details and participate through this link: SEC S20W4 | Introduction to PHP Part - 2.

Cream Colorful Illustrative English Class Presentation.png
Image Source: canva


๐Ÿ” Section 1: Theoretical Questions

๐Ÿš€ Understanding Exceptions in PHP

Exceptions in PHP help manage errors gracefully, allowing your applications to handle unexpected situations without crashing. Here's a basic example of using try and catch blocks to manage a division by zero error:

Division by Zero Error:

Handling Division by Zero Error
Image Placeholder: Handling Division by Zero Error

2.JPG
Image Placeholder: Result when dividing by zero

When dividing with a non-zero number, there is no error:
3.JPG
Image Placeholder: Example of successful division

4.JPG
Image Placeholder: Result of successful division

โš ๏ธ Six Types of Errors in PHP

  1. Parse Error:
    These occur due to syntax errors in your code.
    Parse Error Example
    Image Placeholder: A common parse error

    6.JPG
    Image Placeholder: Output of code

  2. Fatal Error:
    Stops script execution due to critical issues, such as calling undefined functions.
    Fatal Error Example
    Image Placeholder: Example of a fatal error

    8.JPG
    Image Placeholder: Output of code

  3. Warning:
    Indicates potential problems without halting script execution.
    Warning Example
    Image Placeholder: A warning message indicating a non-critical issue

    10.JPG
    Image Placeholder: Output of code with warning

  4. Notice:
    Alerts about less severe issues, often related to variables.
    Notice Example
    Image Placeholder: A notice about variable usage

    13.JPG
    Image Placeholder: Output of code with notice

  5. Deprecated:
    Indicates the use of deprecated functions or features that may be removed in future PHP versions.
    Example:

    
    <?php
    if (ereg("pattern", $string)) {
        // do something
    }
    ?>
    

    Output: Deprecated: Function ereg() is deprecated.

  6. Strict:
    Suggests changes to code that may cause issues in future PHP versions.
    Example:

    
    <?php
    
    function myFunction(
    ) {
        return;
    }
    $result = myFunction() + 5;
    ?>
    

    Output: Strict Standards: Only variables should be passed by reference.

๐Ÿงฉ Personal Errors and Lessons Learned

Errors are part of the learning process. Here are three personal examples from my development journey:

Real-World Algorithm: Student Grade Processing System

Building a student grade processing system offers rich learning opportunities. Let's explore common errors faced:

Student Grade Processing System Diagram
Image Placeholder: Diagram illustrating the student grade processing system

  1. Division by Zero Error:
    Why does the error show? When I pass an empty array, count($student['grades']) will be 0, thus causing the division by zero error.

15.JPG
Image Placeholder: Caught in action: Division by zero error

Capture1.JPG
Image Placeholder: Image of error message for Division by Zero

16.JPG
Image Placeholder: Solution for Division by Zero error

  1. Undefined Array Key Error:
    Why does the error show? If we try to access a student that doesn't exist, we'll get an undefined array key error.

17.JPG
Image Placeholder: Error indicating an undefined array key

18.JPG
Image Placeholder: Error message for Undefined Array Key

19.JPG
Image Placeholder: Solution for Undefined Array Key error

  1. Incorrect Argument Passing:

21.JPG

Image Placeholder: Error due to incorrect argument passing

22.JPG
Image Placeholder: Image of error message for Incorrect Argument Passing

23.JPG
Image Placeholder: Solution for Incorrect Argument Passing

๐Ÿ”— Includes and Their Importance

The include statement in PHP allows you to incorporate code from one file into another, promoting reusability.

Example of Using Include:


// header.php
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>My Website</title>
</head>
<body>
    <header>
        <h1>Welcome to My Website</h1>
    </header>
</body>
</html>

// footer.php
<footer>
    <p>&copy; 2024 My Website</p>
</footer>

๐ŸŒ Database Connection Example

Connecting to a database using PDO is crucial for data management

. Hereโ€™s how itโ€™s done:


<?php
$host = 'localhost';
$dbname = 'mydb';
$username = 'user';
$password = 'password';

try {
    $pdo = new PDO("mysql:host=$host;dbname=$dbname", $username, $password);
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    echo "Connected successfully";
} catch(PDOException $e) {
    echo "Connection failed: " . $e->getMessage();
}
?>

Here's your content with the Image Placeholder format added for each image, positioned below the images as requested:


๐Ÿ” Section 2: Practice Questions

๐Ÿ“š Page with Include

Letโ€™s create a learn.php page using includes to enhance maintainability.

Learn Page Structure
Image Placeholder: Footer included in the learn page.

navbat.JPG
Image Placeholder: Navbar included in the learn page.

Learn Page Header
Image Placeholder: Header included in the learn page.

Learn Page Sidebar
Image Placeholder: Sidebar content for easy navigation.

Main Content of Learn Page
Image Placeholder: The main content of the learn page.

Final Layout of Learn Page
Image Placeholder: Final layout showcasing all components.

๐Ÿ—„๏ธ Database Setup for Registration Page

First, configure the database:

Database Configuration
Image Placeholder: Database configuration settings.

Then, create the students table:

Database Structure
Image Placeholder: Structure of the students table in the database.

๐Ÿ‘ค Registration Page

Hereโ€™s a glimpse of the registration page for new students:

register code.JPG
Image Placeholder: Registration code for the page.

Registration Page Design
Image Placeholder: User-friendly registration page design.

Successful Registration Message

Successful Registration
Image Placeholder: Success message displayed after registration.

Database View Post-Registration

Database After Registration
Image Placeholder: Database view after saving five students.

๐Ÿ”‘ Login Page

Hereโ€™s the login page that verifies user credentials:

Login Code
Image Placeholder: Login code for verification.

Login Page Design
Image Placeholder: Simple and effective login page design.

Bonus: Redirecting to Welcome Page

Redirect Code
Image Placeholder: Code for redirecting to welcome page.

Welcome Page
Image Placeholder: Welcome page displayed after a successful login.


๐ŸŽ‰ Conclusion

This showcase demonstrates the process of developing a PHP-based application, highlighting best practices, error handling, and effective design strategies. Each component plays a critical role in creating a seamless user experience.


I want to invite @khiwa , @shovro and @fauziah95 join the contest.


My introduction post on Steemit: link


About the Author

๐Ÿ‘‹ Hi, Iโ€™m Kafio!
Software Engineer | Data Science Enthusiast | Trading Buff | Development Geek | Computer Science Lover ๐Ÿ’ป๐Ÿ“Š๐Ÿ“ˆ

Iโ€™m passionate about exploring the intersection of technology and innovation, with a special interest in data science, trading, and software development. Whether itโ€™s diving into the latest in computer science or developing new projects, Iโ€™m always excited to learn and share insights. ๐Ÿš€

I also love to travel

Got questions or just want to connect? Feel free to reach out to me at: [email protected] ๐ŸŒŸ

image00001.jpeg

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...
ย  ยท ย last monthย 

Hi @kafio you have presented everything well and in order. Your PDO method to connect the database and PHP file is good. Great job while creating and linking the login page. Your work is appreciative. Best wishes.

ย  ยท ย last monthย 

Thank you so much for your kind word! I really appreciate your feedback on my PDO method and the login page. Itโ€™s always great to hear that my efforts are recognice. If you have any suggestions or tips for improvement, Iโ€™d love to hear them. Best wishes to you too!

ย  ยท ย last monthย 

the post is shared on Twitter: Twitter Link.

zerze.JPG

ย  ยท ย last monthย 

Thank you very much for publishing your post in Teachers and Students community

DescriptionInformation
Verified Userโœ…
Plagiarism Free / AI Article Free
#steemexclusive
โœ…
350+ Wordsโœ…
#club5050โœ…
Bot Freeโœ…

Beneficiaries

#burnsteem25
@ myteacher

no
no

Thank you for making a handy post in our community, we hope the verification from the teaching team will be more detailed to assess your post.

Good luck with your participation.

ย  ยท ย last monthย 

thank you for your feedback

ย  ยท ย last monthย 

Omar, my friend, as you've been telling us that programming is your world, I believe that you do understand this class well. I don't have sufficient knowledge on this subject, so I don't know what to comment on this article of yours. I wish you the best of luck, my friend. I am happy to see you have found joy and are growing on Steem.

Keep up the energy.

ย  ยท ย last monthย 

Thank you, my friend! I won't forget you helping me grow on Steemit.

ย  ยท ย last monthย 

No problem. Enjoy the journey and keep the spirit level good.

ย  ยท ย last monthย 

Hello @kafio I liked your post improving PHP skills. Your examples about errors, like division by zero, are really clear and helpful.

Yes it would be great if you could add a video to show how your code works.Wish you best of luck ๐Ÿค—๐ŸŒผ๐ŸŒธ

ย  ยท ย last monthย 

Thanks a lot for your nice comment! Iโ€™m super happy you liked the examples, they were fun to make. ๐Ÿ˜Š I think adding a video would be a cool idea, Iโ€™ll try to do that next time! Wishing you the best too! ๐ŸŒผ๐ŸŒธ

ย  ยท ย last monthย 

GIF_20240928_152908_070.gif