answersLogoWhite

0

PHP Programming

Questions about the PHP programming language; How and when to use it, implementing techniques, debugging and handling errors, and general questions about PHP itself. For questions about software made with PHP, that do not deal with the manipulating, reading, or learning about the actual code, see the Computer Software category.

845 Questions

How can you declare the variable in PHP?

Variables in PHP do not need to be declared like some languages (e.g. JavaScript).

This is all that needs to be done to assign a variable:

$variable = "Value";

In PHP a null value plus a number value is equals to what result. example null plus 6 equals?

In php mathmatical operations treat null like 0, so any number plus null equals itself.

For example

#!/usr/local/bin/php

printf ("%d\n", null+6);

printf ("%d\n", 6+null);

?>

output:

6

6

Is there a global.asax in PHP?

No, ASAX is for ASP.NET, which is another language.

How do you write login and logout code in php?

Have a session_start(); at the very top of the page in php like this:

<?php

session_start();

?>

Then write a login form:

<form action="login.php" method = "POST">

<input type = "text" name = "username">

<input type = "password" name = "password">

<input type = "submit" value = "submit">

</form>

now start a new page called login.php. Put session_start(); at the top of the page again. Now you'll have two different $_POST variables from the form that got submitted. It's $_POST['username'] and $_POST['password']. the "name" of the input is what the $_POST variable is. And it's a $_POST because of the method of the form. Now you should change the names of the $_POST just to make it easier.

<?php

$username = $_POST['username'];

$password = $_POST['password'];

?>

Now it's time to see if that username and password are in the database

<?php

$mysql = "SELECT COUNT(*) FROM users WHERE username = '$username' AND password = '$password'"; // Select statement to find user

$query = mysql_query($mysql); // Query to see if user exists

$result = mysql_query($query); // Result for query. Should be either 1 or 0.

if ($result != 1)

{

?>

<script>

alert ("That username or password doesn't exist");

</script>

<meta http-equiv="refresh" content = "1; URL = index.php">

<?php // Javascript alert saying that username or password was wrong then loading the page back to the main page.

}

else

{

$_SESSION['username'] = $username; // Setting the variable for the username so now you can use $_SESSION['username'] anywhere on your website to display who is logged in.

}

?>

You have to put session_start(); at the top of every webpage though. Now for the logout. Create a page called logout.php. You can link to this page however you'd like. All you need for this page is:

<?php

session_destroy(); //destroys the current session

<meta http-equiv="refresh" content = "1; URL = index.php"> //Reloads the page back to the index.php

?>

How does implode work in PHP?

implode() is a handy little function that pieces together values of an array by putting a "glue" in between.

Advantages and disadvantages of a MySQL database?

PRO:

fairly easy syntax

good management tool (phpmyadmin)

easy to integrate and use with PHP, Ruby etc.

very common therefore lots of tutorials and howtos

CONS:

performance

bad or missing RDMS features (like postgreSQL or other systems have)

limited capacities storage-wise

How do you reverse a string in PHP without using a function?

Without any function is impossible. So I'll assume you mean any coded function, in which case the predefined function below is your answer.

$string = strrev($string);

What is php plus plus?

PHP++ is an object-oriented version of the PHP programming language. ++ is used in programming to increment a variable by one so it means an improved version of PHP.

How do you create a webmail application with PHP?

Creating a webmail application in PHP involves using the related POP3 or IMAP libraries to connect to the mail system and cookies/sessions to save authentication information.

Php Source code for validation?

If you wish to check the syntax of your source code, you need to run the code through the PHP CLI with the -l option. There are also some online resources to validate the syntax. This will check only the syntax of your file. This will not however detect runtime errors or logic errors. You must absolutely run the script for the best possible code validation.

What is the function die used for in PHP?

The function die() stops the script from running. See the example below:

<?php

die("this text will be outputted on the page");

echo "this text will not be shown due to the script ending when the die() function was called";

?>

How do you delay a PHP script?

To delay a script in seconds you would use the sleep() function as shown below:

<?php

echo "hello";

sleep(5); // delays the script for 5 seconds

echo "finished";

?>

How do you declare a constant?

use:

define("GREETING", "Hello you.", true);

1st parameter is the name for your constant

2nd parameter is the value of that constant

3rd parameter is whether or not you want the constant to be case-insensitive. Default is case sensitive.

http://php.net/manual/en/function.define.php

What is the difference between single quotes and double quotes?

in programming, single quotes are for characters, and double quotes are for string, but in php, javascript, html, css i don't see any difference between the two.

How do you check the length of a string in PHP?

It is fairly simple to check the length of a string in PHP. All you need to do is use the function strlen(). Below is an example of how it is used.

<?php

$my_text = "Hello";

echo strlen($my_text); // outputs 5

?>

How do you count how many results are in a PHP array?

To find out how many results there are in a PHP array all you need to do is use the function count() like I have shown you in the example below.


$my_array = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);

echo count($my_array); // outputs 10

?>

Undefined index in PHP?

Please focus on the line number before the line of the error. There must be an array, with some issues with the index.

How do you merge arrays in PHP?

To merge arrays in PHP all you have to do is use the array_merge() function like shown below:

<?php

$array1 = array("Matt", "Michael", "Justin");

$array2 = array("Janice", "Janet", "Kylie");

$array3 = array_merge($array1, $array2);

?>

One thing to remember when merging arrays is you might be creating duplicate results, if you don't want 2 or more of the same result in the array remember to use the function array_unique() to remove the duplicate results from it!

How can you splice two arrays together in PHP?

$array1 = array("Matt","Michael","Justin");
$array2 = array("Janice","Janet","Kylie");

array_splice($array1,2,0$array2);


We take $array2 and add it to $array1 at location 2 starting at the 0 position of $array2.

How do you write a function with a variable number of input elements in PHP?

When defining your function, do not put any arguments in the definition (e.g. function myFuntion()).

Inside the function, you can use func_num_args() and func_get_arg($number) to get the function's arguments.

How do you stop double logging in using PHP and MySQL?

Assuming you are using a User database and sessions, simply add a Session key. Auto generate this key as follows:

$newsession_key = SHA1( RAND(100000000,999999999));

Enter this in the database on the user row when logging in and store it in a session and in the database. Then on your code where you define the user logged in status you add something like:

$database_session = $DATABASE['sessionkey'];

If($database_session != $_SESSION['sessionkey']){

Unset($_SESSION['username'],$_SESSION['password'],$_SESSION['sessionkey']); #A*

}

A* Add in any sessions you use to keep the user logged in.

How can you check if your PHP script is working on a server?

After you have uploaded it to your web host just go to the page in your browser and then you will see if it is working or not.

What is the difference between POST and REQUEST methods in PHP?

The $_POST array contains only variables supplied by a form that used the POST method, while the $_REQUEST array combines the $_POST, $_GET and $COOKIE arrays.

Does XAMPP support concurrent access?

Xampp is made primarily to be used as a local server. It supports concurrent access to the best of my knowledge, however it will most likely be a slow solution. Access can be granted to multiple machines, however this will only work if the xampp server is active on the host machine. This poses many significant security risks.