Questions tagged [php]

PHP is a widely-used, general-purpose scripting language that is especially suited for web development.

Filter by
Sorted by
Tagged with
1 vote
1 answer
20 views

Laravel Eager Loading tickets from DB

So I have the following relationship in my Model: Events Model: ...
5 votes
2 answers
56 views

Sorting an array by given hierarchy

I have a known hierarchy of the organization units, by descending order: ...
4 votes
1 answer
120 views

Setting default of <options> in Laravel with PHP Match

I have a filter component that lets a user filter for events on a website. The user needs to see what category is currently selected in the filter. So I have an URL with parameters. e.g. ...
-2 votes
0 answers
34 views

Which version of this piece of code is better in readability and semantics [closed]

I've written two different variations of a piece of PHP code. I wonder which one is better in terms of semantics, maintainability, readability, or more prone to mistakes. Version 1 ...
2 votes
1 answer
81 views

Theme picker for Laravel 8 blogging application

I am working on a blogging application in Laravel 8. The application supports themes. In a nutshell, theme support works like this: In the views directory, I have ...
3 votes
2 answers
107 views

Route protection with custom middleware in Laravel 8

I am working on a blogging application in Laravel 8. The application assigns users roles and permissions. There is a many-to-many relationship between roles and permissions. I have created a custom ...
5 votes
1 answer
117 views

Wordpress single post content class customization with if-else function

I have the following code, which displays the content for my custom page template "landingpage.php" and for single posts. The site uses Bootstrap 4. ...
0 votes
0 answers
34 views

PHP How to handle optional array key in if statment [closed]

In PHP there are some good ways to handle values that can be null like null coalescing operator or the null safe operator. Here is one situation I have trouble finding a good solution. Version 1: <...
3 votes
3 answers
4k views

Clean regex matches with named matches

I have a regex pattern that will match some elements from a string and give them a particular name. For example, #^(?<foo>.*)$# will match the whole string ...
4 votes
1 answer
272 views

Adjusting variables on change of status attribute of my Order Laravel model on the updated event in observer

I have an order model and a client model. A client has many orders, an order belongs to a single client. In the client model I keep track of how much money a client paid (...
3 votes
2 answers
2k views

Separate areacode from phone number string with MySQL Database

I have a database with 5000 entries of local area codes. The code uses those records to compare with the area code from a specific phone number string. I'm not sure if the code I use is the fastest ...
1 vote
1 answer
36 views

Select product variations using buttons/swatches instead of a dropdown

In WooCommerce, by default interface, selecting product variations is possible through a dropdown. But, I am developing a site where I wanted buttons/swatches instead of the dropdown. I achieved that ...
7 votes
4 answers
3k views

Converting a query result into an key=>value pairs for a lookup table

My app has a number of lookup tables stored in a MySQL database that are used for various purposes such as a select element. Unfortunately, the query results aren'...
2 votes
1 answer
1k views

Password reset functionality in Codeigniter 3

I am working on an online newspaper/blogging application with CodeIgniter 3.1.8 and Twig. (I use the Twig template engine only for the front-end views). The application, of course, has a registration ...
1 vote
1 answer
113 views

Validate avatar image

I want to know if that code is correct and readable. In class avatar the method getAvatar() validates the data. It checks if the field is not empty and if the photo extension is correct. If not, an ...
3 votes
1 answer
65 views

Multi step form onboarding with sessions and cookies using PHP

My goal is to create a really seamless multi step form for onboarding with only PHP. It stores in sessions and cookies, and retrieves the session data from the cookie if it is stored. This is what I ...
2 votes
1 answer
113 views

Generator for colors

The goal here is to pull an item from an array and when the array ends - to start over - and allow you to pull items infinitely as needed. We don't use PHP that much, so please let us know how we can ...
39 votes
5 answers
10k views

Database connection in constructor and destructor

I am playing with different ways to do database interaction in PHP, and one of the ideas I have been playing with is connecting to the DB in the constructor and disconnecting in the destructor. This ...
4 votes
4 answers
481 views

Shift one value of array values to the beginning

Given an integer $value in the range of 1, 2, 3, I want to get an array that starts with ...
0 votes
2 answers
462 views

Best Practice: creating a nested associative array JSON in PHP

It's been a while I have been on here and I hope I still understand the rules😊. I have recently started learning PHP and WordPress for a Whatsapp Chatbot integration. I have created a webhook on my ...
0 votes
1 answer
53 views

Securely store password in database and then show it on demand? [closed]

I have written a small password manager script for my own purposes. It is hosted locally and cannot be accessed outside my local network. You can see the code on GutHub I know it is better to encrypt ...
2 votes
2 answers
80 views

PHP Router For MVC with strict routing requirements

I had previously asked the question here. The response was pretty much "wow this is bad". So I learned everything I could and wrote what I believe to be better using TDD. The strict ...
2 votes
2 answers
141 views

backup file script in PHP

I'm learning PHP and try to write some scripts as I go along. I wrote this script and it worked. I would like to know if this script can be improved. The purpose of the script below is to prompt the ...
8 votes
1 answer
186 views

Convert integer to Roman numeral string

This is my 1st time developing PHP code and I am only 13 years old. I have experience in other programming languages like lua, python, and c++. This is a simple Number to Roman Numeral code I made for ...
2 votes
1 answer
82 views

PHP Bootstrap Autoloader

I wrote an PHP Autoloader to include it "easyer" to my projects. Here the folder structure: ...
5 votes
2 answers
4k views

Laravel 8 registration and login with user profiles

I am working on a Laravel application (Github repo) that requires user registration and login. After registration, the users can change their registration details (except password, for which there is ...
6 votes
2 answers
482 views

Usage of Factory pattern for maintaing bookshelf having books, magazine and notes

I have tried to achieve the following task using Factory Pattern with PHP version 7.0. Define the classes and their methods (including parameters and return types) for a system that consist of a ...
0 votes
2 answers
48 views

Configuring SQLite3 for best efficiency as a persistent cache

I'm using SQLite3, via php's extension module, to create a persistent object cache for WordPress. It's intended for use on modestly sized sites that don't have access to redis or memcached. Why use ...
0 votes
0 answers
66 views

Eager loading using microservices

I am not quite sure if this is the best approach when fetching relationships, I have this: LinkController.php ...
5 votes
4 answers
1k views

Programming MySQLi prepared statements

I'm programming code with MySQLi prepared statements. I'm a beginner and just want to ask to experts if my code is correct. ...
5 votes
3 answers
2k views

Regex match comments and quotes

UPDATE New regex - covers escaped slashes and uses lazy qualifiers: ...
3 votes
2 answers
291 views

Printing to terminal in a color

Basically, I wish to have 3 printers to print in error state, success state or info state which means nothing but different colors. ...
2 votes
4 answers
447 views

Creating storage path for file from first characters of a hash

I made the following function to create the storage path for files from the the first 5 characters of the file's hash: ...
2 votes
1 answer
150 views

C/C++ hash looking in flat database compared to PHP

I have a hash table which I would like to query as fast as possible. The hash table is stored this way : When I query a hash, I extract first 4 characters then use it to find the file. For instance a ...
0 votes
1 answer
70 views

How to optimize a search between dates in a WP Query?

In the posts I have a date field and a start and end date range. I need to query if it matches date or if it matches between the dates in the range. This Query works, but how can I optimize? ...
5 votes
2 answers
909 views

Formatting Credit card names from API response

I have an API that returns the brand of a credit card. I want to make them look nice for my user (e.g. correct case and spacing). The possible options are amex, <...
2 votes
1 answer
1k views

Instagram Basic Display API Full Example PHP

Here's an example script I wrote for Instagram (Facebook)'s Basic Display API. I did this for myself because I haven't used Instagram's API since their Legacy API was disabled, and I needed to ...
1 vote
2 answers
2k views

Formatting date difference in human-friendly terms

I've written a function to take the difference between two dates and display it in a particular format. I've used a lot of if-else statements, which seems a little ...
0 votes
0 answers
113 views

Pay with PayPal through cURL and verify

As All PayPal PHP SDK deprecated, I wrote a simple PHP class to make payment with PayPal through cURL and verifying it. Note : I didn't add shipment details for payment because didn't need that. For ...
3 votes
2 answers
85 views

First attempt at a distance object

In our code we deal with distances a lot, a distance being a number and unit eg 15km, or 1.234mi The current approach is to use 2 functions, convert_distance(), and ...
3 votes
1 answer
204 views

Diving into PHP's MVC - the application's core

I recently started programming in PHP using the MVC-pattern, which resulted in one of best choices I made so far, but I read through many articles showing "easy" examples and some of them ...
2 votes
2 answers
135 views

Router and solid principle

I would like to know if my Routing system respects the solid principle ? The system is simple, Router class contains the routes and returns the correct route and Route class represents a route, ...
5 votes
2 answers
701 views

A PHP script used on my home server to download YouTube videos

I have a PHP page I used to download YouTube videos, hosted on my Pi 4. The site is only going to be used on my home network (so a dot local address), but it allows me to download videos on my phone ...
4 votes
2 answers
2k views

Full text search for multiple words using Laravel and MySQL

I'm using Laravel 5.6 and MySQL. I need to search multiple columns for a single word or multiple words (e.g., "camhandle", "cam handle"). The way I was originally doing it was just ...
1 vote
2 answers
89 views

PHP function that checks if user should have access based on button click and yearly quarter

I have a function that has a few and/or operators in it. I'm sure there is a better way to write this but can't seem to figure it out. I thought I could use arrays but ran into an issue with having ...
3 votes
2 answers
64 views

delete multidimentional array value in php

I made a program that converts JSON to a PHP array and checks every value of the multidimensional array with a loop and if a value matches certain values then it deletes that key and value and ...
3 votes
3 answers
606 views

Bool function in PHP to check if you run the script inside docker

I had to do a CLI script that does something, I also had to add a validation that checks if it runs inside the Docker. After some research I found a solution that people check some processes via ...
38 votes
7 answers
104k views

Setting a default value if a variable is empty

This is the pattern I've been using for "if the variable is empty, set a default value", and for variables like $muffin, it has all seemed well and good. But in ...
3 votes
1 answer
86 views

PHP Laravel Check if given date is a week or a regular day

I have a problem. In my project the user can copy a menu for a specific day or for a specific week. The value that will be sent to the back-end will be something like: ...
2 votes
1 answer
87 views

Efficiently use array values and variables in a WordPress template

Is it better to validate and put an array value into a variable at the top of a file or to check the array value directly right before output in the file? I work at a WordPress shop and we have a ...

1
2 3 4 5
87