PHP
Completed
-
Section 1: Getting Started
-
Section 2: Core Syntax
-
Section 3: Functions
-
Section 4: Object-Oriented PHP
-
Section 5: Working with Data
-
Section 6: Modern PHP (PHP 8)
-
Section 7: Working with Files and Networking
-
Section 8: Common PHP Frameworks Overview
-
Section 9: Tooling and Ecosystem
-
Section 10: Practical Projects
-
Section 11: Interview Practice
-
Section 12: More Standard Library
-
Section 13: Data Structures and Algorithms in PHP
-
Section 14: More Practice Exercises
-
Section 15: More Security and Best Practices
-
Section 16: More Testing and Tooling
-
Section 17: WordPress-Style CMS Concepts
-
Section 18: Advanced OOP Practice
-
Section 19: More Web Fundamentals
-
Section 20: Database Practice
-
Section 21: PHP Manual: Array Functions
-
Section 22: PHP Manual: Date and Calendar Functions
-
Section 23: PHP Manual: Filesystem and Directory Functions
-
Section 24: PHP Manual: Filter and Var Handling
-
Section 25: PHP Manual: Math Functions
-
Section 26: PHP Manual: JSON and XML
-
Section 27: PHP Manual: Network and Stream Functions
-
Section 28: PHP Manual: Error and Exception Handling
-
Section 29: PHP Manual: Output Control and Misc
-
Section 30: PHP Manual: FTP, Zip, and Mail
-
Section 31: Modern PHP Frameworks Deep Dive
-
Section 32: PHP Design Patterns
-
Section 33: More Practice Exercises
-
Section 34: PHP Performance and Deployment
-
Section 35: More Interview Practice
-
Section 36: More PHP Standard Library
-
Section 37: PHP Concurrency and Async
-
Section 38: More Web Development Practice
-
Section 39: PHP Testing Deep Dive
-
Section 40: Composer and Package Development
-
Section 41: PHP Security Deep Dive
-
Section 42: More Practical Projects
-
Section 43: Legacy PHP Maintenance
-
Section 44: More Algorithm Practice
-
Section 45: Final Practice and Review
-
Section 46: PHP for E-Commerce Patterns
-
Section 47: PHP API Design Deep Dive
-
Section 48: PHP Caching Strategies
-
Section 49: PHP Queue and Background Jobs
-
Section 50: PHP Multi-Tenancy Patterns
-
Section 51: PHP Real-Time Features
-
Section 52: PHP CMS and Content Modeling
-
Section 53: PHP Internationalization
-
Section 54: More Framework-Specific Practice
-
Section 55: PHP Legacy Code Refactoring
-
Section 56: More Practice Projects Round 2
-
Section 57: PHP Command-Line Applications
-
Section 58: PHP and Microservices
-
Section 59: More Interview and Review Round 2
225: RTL Support in PHP-Rendered Pages
A few years ago, I was reviewing a PR for a colleague who was expanding a SaaS platform into the Middle Eastern market. He had spent nearly two days manually tweaking CSS margins and floats to "fix" the layout for the Arabic version of the site. He was essentially writing a second, mirrored version of the entire stylesheet by hand. When I asked why he wasn't using the dir attribute, he looked at me like I was speaking a foreign language. He had forgotten that the browser is actually designed to handle the heavy lifting of Right-to-Left (RTL) mirroring if you just give it the right hint in the HTML.
As a PHP developer, your job isn't to write the CSS that flips the page, but to build the logic that tells the browser which direction the content should flow. If you're rendering pages dynamically based on a user's language preference, you can't hardcode dir="ltr". You need a system that detects the locale and injects the correct attributes into your template.
Mapping Locales to Document Direction
Not every language is Left-to-Right, and not every non-English language is RTL. You'll want to create a helper function or a configuration array that maps ISO language codes to their respective directions. I usually keep this in a localization helper class. For example, while English (en) and French (fr) are ltr, Arabic (ar), Hebrew (he), and Persian (fa) must be rtl.
$rtl_languages = ['ar', 'he', 'fa', 'ur']; $user_lang = $user->getLanguage(); // Assume this returns 'ar' $dir = in_array($user_lang, $rtl_languages) ? 'rtl' : 'ltr'; ?> <html lang="" dir="">By placing the
dirattribute on the<html>tag, you set the global direction for the entire document. This automatically flips the alignment of text, the order of columns in a simple layout, and the behavior of scrollbars. It's a massive win for very little code.Integrating Directional Logic with Stylesheets
While
dir="rtl"handles the basics, you'll inevitably run into elements that need a specific touch—like an icon that represents "forward" (an arrow pointing right in LTR, but left in RTL). I've found that the cleanest way to handle this in PHP is to inject a direction-specific class into the body tag.<body class="theme-dark ">With that class in place, you can avoid writing entirely separate CSS files. Instead, you can use CSS Logical Properties. Instead of using
margin-left, usemargin-inline-start. The browser will automatically interpret "start" as "left" for LTR and "right" for RTL. If you're supporting older browsers that don't recognize logical properties, that.rtl-modeclass allows you to override specific stubborn elements without polluting your main layout logic.One last tip: be careful with "hard" directions. If you have a phone number or a credit card field, those should almost always remain LTR, regardless of the page direction. You can wrap those specific PHP-rendered inputs in a
<span dir="ltr">to keep them from flipping and confusing the user.
📋 Practical Task
Building a Locale-Aware Page Wrapper
Create a PHP script that simulates a user session with a language preference. Your task is to implement the following:
- Create an array of RTL languages (at least Arabic, Hebrew, and Persian).
- Write a logic block that checks a variable
$current_user_langagainst that array. - Render a complete HTML skeleton where the
<html>tag dynamically receives the correctlanganddirattributes. - Add a
<body>tag that receives a class of eitherdir-rtlordir-ltrbased on the result. - Inside the body, render a simple navigation menu (using a
<ul>) and a "User ID" field. Ensure the User ID field is wrapped in a way that it remainsltreven if the rest of the page isrtl.
There are no comments for now.