Questions tagged [php]

PHP is an open-source, multi-paradigm, dynamically-typed, and interpreted scripting language designed initially for server-side web development. Use this tag for questions about programming in the PHP language.

php
Filter by
Sorted by
Tagged with
101 votes
7 answers
29k views

PHP global in functions

What is the utility of the global keyword? Are there any reasons to prefer one method to another? Security? Performance? Anything else? Method 1: function exempleConcat($str1, $str2) { return $...
Pascal Qyy's user avatar
  • 4,462
119 votes
10 answers
104k views

Grabbing the href attribute of an A element

Trying to find the links on a page. my regex is: /<a\s[^>]*href=(\"\'??)([^\"\' >]*?)[^>]*>(.*)<\/a>/ but seems to fail at <a title="this" href="that">what?</a> ...
bergin's user avatar
  • 1,584
82 votes
12 answers
200k views

How to change mysql to mysqli?

Based on this code below I use for regular mysql, how could I convert it to use mysqli? Is it as simple as changing mysql_query($sql); to mysqli_query($sql);? <?PHP //in my header file that is ...
JasonDavis's user avatar
  • 48.6k
2278 votes
7 answers
458k views

How does PHP 'foreach' actually work?

Let me prefix this by saying that I know what foreach is, does and how to use it. This question concerns how it works under the bonnet, and I don't want any answers along the lines of "this is how you ...
DaveRandom's user avatar
  • 88.3k
406 votes
20 answers
1.2m views

Invalid argument supplied for foreach()

It often happens to me to handle data that can be either an array or a null variable and to feed some foreach with these data. $values = get_values(); foreach ($values as $value){ ... } When you ...
Roberto Aloi's user avatar
  • 30.8k
360 votes
11 answers
381k views

How to use multiple databases in Laravel

I want to combine multiple databases in my system. Most of the time the database is MySQL; but it may differ in future i.e. Admin can generate such a reports which is use source of heterogeneous ...
Chintan7027's user avatar
  • 7,305
261 votes
8 answers
429k views

PHP - Debugging Curl

I'd like to see what the post fields in the request are before I send it. (For debugging purposes). The PHP library (class) I am using is already made (not by me), so I am trying to understand it. ...
Matthew's user avatar
  • 15.4k
566 votes
18 answers
2.1m views

Convert a date format in PHP [duplicate]

I am trying to convert a date from yyyy-mm-dd to dd-mm-yyyy (but not in SQL); however I don't know how the date function requires a timestamp, and I can't get a timestamp from this string. How is ...
matthy's user avatar
  • 8,244
119 votes
11 answers
528k views

How to create cron job using PHP?

I'm new to using cron job. I don't even know how to write it. I have tried to search from internet, but I still don't understand it well. I want to create a cron job that will execute my code every ...
user2738520's user avatar
  • 1,351
600 votes
18 answers
428k views

How do I catch a PHP fatal (`E_ERROR`) error?

I can use set_error_handler() to catch most PHP errors, but it doesn't work for fatal (E_ERROR) errors, such as calling a function that doesn't exist. Is there another way to catch these errors? I am ...
too much php's user avatar
  • 89.9k
470 votes
6 answers
1.4m views

Showing all errors and warnings [duplicate]

UPDATE 2: I have now removed the following from the .php file: <?php error_reporting( E_ALL ); ?> I have set display_erros in php.ini as follows: display_errors = On Error reporting is ...
oshirowanen's user avatar
  • 15.7k
25 votes
10 answers
12k views

How to access and manipulate multi-dimensional array by key names / path?

I've to implement a setter in PHP, that allows me to specify the key, or sub key, of an array (the target), passing the name as a dot-separated-keys value. Given the following code: $arr = array('a' =&...
Kamafeather's user avatar
  • 9,271
2802 votes
31 answers
2.0m views

How do I get a YouTube video thumbnail from the YouTube API?

If I have a YouTube video URL, is there any way to use PHP and cURL to get the associated thumbnail from the YouTube API?
CodeOverload's user avatar
  • 47.8k
257 votes
11 answers
602k views

Cross-Origin Request Headers(CORS) with PHP headers

I have a simple PHP script that I am attempting a cross-domain CORS request: <?php header("Access-Control-Allow-Origin: *"); header("Access-Control-Allow-Headers: *"); ... Yet I still get the ...
user avatar
87 votes
3 answers
116k views

Replace preg_replace() e modifier with preg_replace_callback

I'm terrible with regular expressions. I'm trying to replace this: public static function camelize($word) { return preg_replace('/(^|_)([a-z])/e', 'strtoupper("\\2")', $word); } with ...
Casey's user avatar
  • 1,971
789 votes
24 answers
1.8m views

Show a number to two decimal places

What's the correct way to round a PHP string to two decimal places? $number = "520"; // It's a string from a database $formatted_number = round_to_2dp($number); echo $formatted_number; The output ...
Rich Bradshaw's user avatar
345 votes
21 answers
767k views

How to get time difference in minutes in PHP

How to calculate minute difference between two date-times in PHP?
Tom Smykowski's user avatar
299 votes
10 answers
242k views

Why check both isset() and !empty()

Is there a difference between isset and !empty. If I do this double boolean check, is it correct this way or redundant? and is there a shorter way to do the same thing? isset($vars[1]) AND !empty($...
silow's user avatar
  • 4,286
83 votes
5 answers
297k views

Call php function from JavaScript

Is there a way I can run a php function through a JS function? something like this: <script type="text/javascript"> function test(){ document.getElementById("php_code").innerHTML="<?php ...
Jason Russell's user avatar
90 votes
21 answers
48k views

When is eval evil in php?

In all the years I have been developing in PHP, I have always heard that using eval() is evil. Considering the following code, wouldn't it make sense to use the second (and more elegant) option? If ...
Pierre Spring's user avatar
287 votes
23 answers
281k views

php execute a background process

I need to execute a directory copy upon a user action, but the directories are quite large, so I would like to be able to perform such an action without the user being aware of the time it takes for ...
tim's user avatar
  • 3,276
93 votes
6 answers
43k views

Are global variables in PHP considered bad practice? If so, why? [duplicate]

function foo () { global $var; // rest of code } In my small PHP projects I usually go the procedural way. I generally have a variable that contains the system configuration, and when I nead ...
KRTac's user avatar
  • 2,827
424 votes
18 answers
352k views

Website screenshots

Is there any way of taking a screenshot of a website in PHP, then saving it to a file?
user avatar
416 votes
14 answers
132k views

Why would one omit the close tag?

I keep reading it is poor practice to use the PHP close tag ?> at the end of the file. The header problem seems irrelevant in the following context (and this is the only good argument so far): ...
johnlemon's user avatar
  • 21.2k
61 votes
4 answers
33k views

How to squeeze error message out of PDO? [duplicate]

I can't seem to get any error message from PDO: #$dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING ); try { $sth = $dbh->prepare('@$%T$!!!'); print_r($sth); print_r($dbh->...
Your Common Sense's user avatar
1191 votes
4 answers
215k views

What are Long-Polling, Websockets, Server-Sent Events (SSE) and Comet?

I have tried reading some articles, but I am not very clear on the concepts yet. Would someone like to take a shot at explaining to me what these technologies are: Long Polling Server-Sent Events ...
user1437328's user avatar
  • 15.7k
251 votes
19 answers
418k views

PHP - SSL certificate error: unable to get local issuer certificate

I'm running PHP Version 5.6.3 as part of XAMPP on Windows 7. When I try to use the Mandrill API, I'm getting the following error: Uncaught exception 'Mandrill_HttpError' with message 'API call to ...
Dor Dadush's user avatar
  • 2,543
44 votes
6 answers
21k views

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result [duplicate]

I get the error when trying to run this: <?php require_once('includes/DbConnector.php'); $connector = new DbConnector(); $result = $connector->query('SELECT title,content FROM staff_vacancies ...
user96828's user avatar
  • 513
310 votes
34 answers
509k views

How to convert array to SimpleXML

How can I convert an array to a SimpleXML object in PHP?
user avatar
329 votes
15 answers
609k views

JSON encode MySQL results

How do I use the json_encode() function with MySQL query results? Do I need to iterate through the rows or can I just apply it to the entire results object?
Trick Jarrett's user avatar
144 votes
14 answers
474k views

Multiple file upload in php

I want to upload multiple files and store them in a folder and get the path and store it in the database... Any good example you looked for doing multiple file upload... Note: Files can be of any ...
udaya's user avatar
  • 9,658
1139 votes
16 answers
627k views

What is the difference between public, private, and protected?

When and why should I use public, private, and protected functions and variables inside a class? What is the difference between them? Examples: // Public public $variable; public function ...
Adam Halasz's user avatar
  • 57.9k
405 votes
43 answers
1.3m views

PDOException “could not find driver”

I have just installed Debian Lenny with Apache, MySQL, and PHP and I am receiving a PDOException could not find driver. This is the specific line of code it is referring to: $dbh = new PDO('mysql:...
Mike Moore's user avatar
  • 7,388
338 votes
17 answers
691k views

Passing an array to a query using a WHERE clause

Given an array of ids $galleries = array(1,2,5) I want to have a SQL query that uses the values of the array in its WHERE clause like: SELECT * FROM galleries WHERE id = /* values of array $galleries....
user avatar
193 votes
9 answers
126k views

MySQL Great Circle Distance (Haversine formula)

I've got a working PHP script that gets Longitude and Latitude values and then inputs them into a MySQL query. I'd like to make it solely MySQL. Here's my current PHP Code: if ($distance != "Any" &...
Nick Woodhams's user avatar
142 votes
11 answers
44k views

Is there a use-case for singletons with database access in PHP?

I access my MySQL database via PDO. I'm setting up access to the database, and my first attempt was to use the following: The first thing I thought of is global: $db = new PDO('mysql:host=127.0.0.1;...
seriousdev's user avatar
  • 7,576
504 votes
17 answers
628k views

How can one use multi threading in PHP applications

Is there a realistic way of implementing a multi-threaded model in PHP whether truly, or just simulating it. Some time back it was suggested that you could force the operating system to load another ...
Steve Obbayi's user avatar
  • 6,055
97 votes
5 answers
97k views

How to properly set up a PDO connection

From time to time I see questions regarding connecting to database. Most answers is not the way I do it, or I might just not get the answers correctly. Anyway; I've never thought about it because the ...
ThomasK's user avatar
  • 2,220
7 votes
2 answers
5k views

Seo Friendly URL results in CSS IMG and JS not working

I want to make my URL as SEO Friendly URL. I tried editing .htaccess file by rewriting rule RewriteRule ^swift-details/([0-9]+)/([0-9a-zA-Z_-]+)$ swift-details.php?id=$1 [NC,L] RewriteRule ^swift-...
user avatar
267 votes
36 answers
352k views

Good PHP ORM Library? [closed]

Is there a good object-relational-mapping library for PHP? I know of PDO/ADO, but they seem to only provide abstraction of differences between database vendors not an actual mapping between the ...
230 votes
9 answers
292k views

Using dynamic variable names in PHP

I'm trying to use dynamic variable names. So inside this loop I want to create $file0, $file1, and $file2: for($i=0; $i<=2; $i++) { $("file" . $i) = file($filelist[$i]); } var_dump($...
user1159454's user avatar
  • 3,287
64 votes
4 answers
148k views

What is parameterized query? [duplicate]

What is a parameterized query, and what would an example of such a query be in PHP and MySQL?
totalnoobs's user avatar
337 votes
32 answers
571k views

Error “Target class controller does not exist” when using Laravel 8

Here is my controller: <?php namespace App\Http\Controllers\Api; use App\Http\Controllers\Controller; use Illuminate\Http\Request; class RegisterController extends Controller { public ...
Nguyễn Việt Dũng's user avatar
76 votes
8 answers
138k views

How to create friendly URL in php?

Normally, the practice or very old way of displaying some profile page is like this: www.domain.com/profile.php?u=12345 where u=12345 is the user id. In recent years, I found some website with very ...
Peter's user avatar
  • 1,083
82 votes
6 answers
162k views

Error message "Strict standards: Only variables should be passed by reference"

$el = array_shift($instance->find(..)) The above code somehow reports the strict standards warning, but this will not: function get_arr(){ return array(1, 2); } $el = array_shift(get_arr()); ...
user198729's user avatar
  • 62.8k
36 votes
7 answers
57k views

How can I bind an array of strings with a mysqli prepared statement?

I need to bind an array of values to WHERE IN(?) clause. How can I do that? This works: $mysqli = new mysqli("localhost", "root", "root", "db"); if(!$mysqli || $...
Mark's user avatar
  • 2,686
1018 votes
69 answers
1.8m views

PHP random string generator

I'm trying to create a randomized string in PHP, and I get absolutely no output with this: <?php function RandomString() { $characters = '...
Captain Lightning's user avatar
419 votes
22 answers
779k views

How to set up file permissions for Laravel?

I'm using Apache Web Server that has the owner set to _www:_www. I never know what is the best practice with file permissions, for example when I create new Laravel 5 project. Laravel 5 requires /...
Robo Robok's user avatar
  • 21.8k
788 votes
36 answers
2.1m views

Fatal Error: Allowed Memory Size of 134217728 Bytes Exhausted

I have a bunch of client point of sale (POS) systems that periodically send new sales data to one centralized database, which stores the data into one big database for report generation. The client ...
ArcticZero's user avatar
  • 7,921
744 votes
11 answers
760k views

Formatting a number with leading zeros in PHP [duplicate]

I have a variable which contains the value 1234567. I would like it to contain exactly 8 digits, i.e. 01234567. Is there a PHP function for that?
Andromeda's user avatar
  • 12.8k