69 Answered Questions for the topic PHP
Php
05/28/19
Pass a PHP string to a JavaScript variable (and escape newlines)?
What is the easiest way to encode a PHP string for output to a JavaScript variable?
I have a PHP string which includes quotes and newlines. I need the contents of this string to be put into a...
more
Php
05/28/19
Get the full URL in PHP?
I use this code to get the full URL:
$actual_link = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'];
The problem is that I use some masks in my `.htaccess`, so what we see in the URL is...
more
Php
05/28/19
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):...
more
Php
05/28/19
How do I catch a PHP Fatal 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...
more
05/28/19
What is the point of interfaces in PHP?
Interfaces allow you to create code which defines the methods of classes that implement it. You cannot however add any code to those methods. Abstract classes allow you to do the same thing, along...
more
Php
05/28/19
How do I send a POST request with PHP?
Actually I want to read the contents that come after the search query, when it is done. The problem is that the URL only accepts `POST` methods, and it does not take any action with `GET`...
more
Php
05/26/19
Fastest way to check if a string is JSON in PHP?
I need a really, really fast method of checking if a string is JSON or not. I feel like this is not the best way:
function isJson($string) {
return ((is_string($string) &&
...
more
Php
05/26/19
Is there an easy way to delete an element from an array using PHP?
Is there an easy way to delete an element from an array using PHP, such that `foreach ($array)` no longer includes that element?
I thought that setting it to `null` would do it, but apparently it...
more
05/24/19
How would you access Object properties from within an object method?
What is the "purist" or "correct" way to access an object's properties from within an object method that is not a getter/setter method?I know that from outside of the object you should use a...
more
Php
05/22/19
How to enable PHP short tags?
I have a web application on a Linux server which starts with `<?`
I needed to copy this application to a windows environment and everything is working fine except that an SQL statement is being...
more
Php
05/22/19
What is the difference between client-side and server-side programming?
I have this code:
<script type="text/javascript">
var foo = 'bar';
<?php
file_put_contents('foo.txt', ' + foo + ');
?>
var baz = <?php echo 42; ?>;
...
more
Php
05/21/19
What is the difference between single-quoted and double-quoted strings in PHP?
I'm not an expert in PHP programming, but I'm a little confused why I see some code in PHP with string placed in single quotes and sometimes in double quotes.
I just know in .NET, or the C...
more
Php
05/20/19
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 =...
more
Php
05/18/19
How to convert date to timestamp in PHP?
How do I get timestamp from e.g. `22-09-2008`?
Php
05/17/19
Returning JSON from a PHP Script?
I want to return JSON from a PHP script.
Do I just echo the result? Do I have to set the `Content-Type` header?
Php
05/15/19
How to add elements to an empty array in PHP?
If I define an array in PHP such as (I don't define its size):
<!-- language: lang-php -->
$cart = array();
Do I simply add elements to it using the following?
<!-- language:...
more
Php
05/15/19
Why shouldn't I use mysql_* functions in PHP?
What are the technical reasons for why one shouldn't use `mysql_*` functions? (e.g. `mysql_query()`, `mysql_connect()` or `mysql_real_escape_string()`)?
Why should I use something else even if...
more
Php
05/14/19
How to get the client IP address in PHP?
How can I get the client IP address using PHP?
I want to keep record of the user who logged into my website through his/her IP address.
Php
05/14/19
NOW() function in PHP?
Is there a PHP function that returns the date & time in the same format as the MySQL function `NOW()`?
I know how to do it using `date()`, but I am asking if there is a function only for...
more
Php
05/14/19
Get the first element of an array in PHP?
I have an array:
`array( 4 => 'apple', 7 => 'orange', 13 => 'plum' )`
I would like to get the first element of this array. Expected result: <i>string</i> `apple`
One...
more
How to perform an action every 5 results?
How can I perform an action within a `for` loop every 5 results?
Basically I'm just trying to emulate a table with 5 columns.
Php
05/13/19
How do I check if a directory exists via PHP?
I want to create a directory if it does'nt exist.
Is using `is_dir` enough for that purpose?
if (!is_dir($dir)) {
mkdir($dir);
}
Or should I combine `is_dir` with `file_exists`?
...
more
Php
05/06/19
How do the PHP equality (== double equals) and identity (=== triple equals) comparison operators differ?
What is the difference between `==` and `===`?
- How exactly does the loosely `==` comparison work?
- How exactly does the strict `===` comparison work?
What would be some useful examples?
Php
05/05/19
PHP equivalent of .NET/Java's toString()?
How do I convert the value of a PHP variable to string?
I was looking for something better than concatenating with an empty string:
$myText = $myVar . '';
Like the `ToString()` method in Java...
more
Still looking for help? Get the right answer, fast.
Ask a question for free
Get a free answer to a quick problem.
Most questions answered within 4 hours.
OR
Find an Online Tutor Now
Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.