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

What function count elements in an array?

Using the count() method:

$a[0] = 1;

$a[1] = 3;

$a[2] = 5;

$result = count($a);

// $result 1

?>

What database does the WAmmp server refer to when it's used with PHP?

WAMP is an abbreviation of "Windows, Apache, MySQL, and PHP", so it uses MySQL database server.

How do you upload function in php?

If you mean uploading files...

HTML Form (front-end):


enctype="multipart/form-data">






PHP (upload_file.php)


if ($_FILES["file"]["error"] > 0)
??{
??echo "Error: " . $_FILES["file"]["error"] . "
";

??}
else
??{
??echo "Upload: " . $_FILES["file"]["name"] . "
";

??echo "Type: " . $_FILES["file"]["type"] . "
";

??echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB
";

??echo "Stored in: " . $_FILES["file"]["tmp_name"];
??}
?>

Thank You Google & w3schools (http://www.w3schools.com/php/php_file_upload.asp)

What is a valid variable declaration in PHP?

In PHP, you can declare a variable in several ways.

In the global scope, or within a function, you can use:

  • var $variable; // Creates an empty variable
  • $variable = {something}; // declares, then assigns, the variable

In the object scope, a property operates like a variable:

  • public $variable; // Creates an empty property
  • private $variable; // Same thing, but can't be referenced outside the object
  • protected $variable; // Similar, but can only be referenced in the object or any object extended by it
  • public/private/protected $variable = {something}; // declares, then assigns a default value to, the property
  • $this->variable = {something}; // if not declared, this will declare the property and assign the value to it

You can also create object properties from outside the object:

  • $object->variable = {something}; // works just like $this->variable

Finally, the arguments to a function are implicitly declared:

  • function DoSomething ($variable) { ... } // declares and assigns $variable

Variables in PHP are very flexible, mutable things. This is unlike some other languages that rely on strict type declarations for variables and other code constructs.

Are the php functions overloading allowed?

You cannot overload PHP functions. Function signatures are based only on their names and do not include argument lists, so you cannot have two functions with the same name. Class method overloading is different in PHP than in many other languages. PHP uses the same word but it describes a different pattern.


You can, however, declare a variadic function that takes in a variable number of arguments. You would use func_num_args() and func_get_arg() to get the arguments passed, and use them normally.


For example:


// *******************************************

function myFunc() {

for ($i = 0; $i < func_num_args(); $i++) {

printf("Argument %d: %s\n", $i, func_get_arg($i));

}

}


/*

Argument 0: a

Argument 1: 2

Argument 2: 3.5

*/

myFunc('a', 2, 3.5);

// ********************************************

What is php is all about?

PHP is a server side programming language that also serves as a general purpose programming languages. What does I mean by Server Side? Well this means that the php source files are being run on the server as a opposed to the end user using their browser to run Javascript code which is a client-side scripting language. PHP allows for the creation of dynamic and interesting website design

What does the acronym php stand for?

PHP stands for Hypertext Preprocessor. This may sound a bit confusing, but it is considered a "recursive acronym" which means that the first word of the acronym is the actual acronym.

What does Index PHP stand for?

PHP stands for Hypertext Preprocessor is a widely-used multipurpose scripting language that is used mainly for dynamic web page development that can be embedded in HTML format.

What does curl stand for in php prgramming?

cURL - A Command line tool for getting or sending files using URL syntax

Why use wamp on php?

For newbies wamp is the better cause folders are available for browsing as one may not know the correct url path. Also modifications are possible with right-click and changing a few settings. As you get accustomed it is better to switch to xampp which is much better I believe.

How do you write coding php using switch case?

The case structure in PHP uses the following syntax:

switch($foo){

case 'bar':

doSomething();

break;

case 'blah':

doSomethingElse();

break;

default:

handleOtherCases();

}

How do you sort array in php without use of sort function?

$arr=array(2,5,4,6,7,8,1);

for($i=0;$i<count($arr);$i++)

{

for($j=$i;$j<count($arr);$j++)

{

if($arr[$i] > $arr[$j])

{

$temp=$arr[$i];

$arr[$i]=$arr[$j];

$arr[$j]=$temp;

}

}

}

How do you write homepage module in php?

A home page can be any php script. The most common used name for the 'home page' is the file called index.php in the public_html folder of your web host.

How do you write a program to print Inverse number in PHP?

There are two types of inverse numbers in this context: additive and multiplicative.

An additive inverse number is a number that's had its sign flipped: positive becomes negative, and negative becomes positive. The PHP code for this would be:

$result=-$number;

A multiplicative inverse number is a number that's been divided into 1. So 5 becomes 1/5, and 1/5 becomes 1/1/5, or 5. The PHP code for this would be:

$result=1/$number;

How do you print 1 to 100 without using loop in php?

The best way to print the numbers 1 to 100 in PHP without using a loop is with the following code:

echo implode("<br>", range(1,100));

You can replace the <br> with anything that you want to separate the numbers, such as dashes. I used a line-break in the example.

What is the function exit used for php?

In PHP, the exit() function calls and end to the script in much the same way as when the end of the script is reached naturally. Objects are destructed, garbage cleaned, and everything is wrapped up nicely and neatly. This is as opposed to die(), which just stops the script where it is without allowing other final processes to complete.

How do you create php web counter and prevent it from counting when returned to the subpages?

I made a few weeks ago:

session_start(); //you can also use a cookie, I prefer session.

error_reporting(0); //to avoid notice in checking if counted.

if(!isset($_SESSION['cont'])) { //check if not counted yet.

error_reporting(E_ALL); //activate error reporting again.

$dir = 'PATH FOR THE VAR TO BE SOTRED IN'; //THIS MUST BE A PHP file !!

if(include($dir)) {

if(isset($cnt)) {

$cnt ++;

$cntr = '<?php'.sprintf(PHP_EOL).' $cnt = '.$cnt.';'.sprintf(PHP_EOL).' ?>';

file_put_contents($dir, $cntr);

$_SESSION['cont'] = true;

} else {

$cnt = 1;

$cntr = '<?php'.sprintf(PHP_EOL).' $cnt = '.$cnt.';'.sprintf(PHP_EOL).' ?>';

file_put_contents($dir, $cntr);

$_SESSION['cont'] = true;

}

} else {

echo 'Wrong file path '.$dir;

}

}

Good luck :)

What is use of dot in php?

The dot (.) operator in PHP is used to concatenate strings. For instance:

$start = "Big";

$end = "Bird";

echo $start . ' ' . $end;

This code would produce the output:

Big Bird

How do you use constants in php?

Constants are simple pieces of data which cannot be changed during PHP script execution. It is useful for storing data which should remain unchanged, like a maximum or minimum value of a variable, etc.

Before first use, constant should be defined: ...

define("MY_MAX_VALUE", 505);

... After this, you can use it:

...

if ($my_variable < MY_MAX_VALUE)

{

... (some code here)

}

...

How do you comment in PHP?

$this is not a comment;

//this is a comment


/*this is also



a



comment */

What is difference between include and require in php?

include will continue executing the script even with errors while require will produce a fatal error and stop the script.

Note: if you put include/require 'grandpa.php' in 'father.php' and then include 'father.php' in 'son.php' and also put include/require 'grandpa.php' in 'son.php' then both require & include will produce errors.

Best thing to add is include_once or require_once in your code as it will only add the file once and will override other include_once or require_once

How can we upload in php?

this is file of index.php or any file that contains uploading area .......

....................................................................................

<body>

<form name="frmFile" method="post" action="fileuploadprocess.php" enctype="multipart/form-data">

<label>Select File</label>

<input type="file" name="flnFile" />

<br />

<input type="submit" value="Upload File" />

</form>

</body>

.....................................................................................................

fileuploadprocess.php that is in form action="fileuploadprocess.php"

<?php

//print_r($_FILES);

//echo(phpinfo());

$sType=$_FILES['flnFile']['type'];

echo($sType);

//echo("<br>");

$arrFileTypes=array("image/jpeg","image/png","image/gif");

$iFileSize=$_FILES['flnFile']['size'];

// For KBs

$iFileSize=$iFileSize/1024;

// For MBs

//$iFileSize=$iFileSize/1024;

echo($iFileSize);

echo("<br>");

//if($sType=="image/jpeg" $sType=="image/png" //$sType=="image/gif")

if($iFileSize<=100)

{

if(in_array($sType,$arrFileTypes))

{

$sDirectory="Uploadfiles/sajid";

if(is_dir($sDirectory))

{

}

else

{

mkdir($sDirectory,0777);

}

$dDateTime=date('mdyhis');

$sTempLocation=$_FILES['flnFile']['tmp_name'];

//$sDesignationLocation="Uploadfiles/".$dDateTime.$_FILES['flnFile']['name'];

$sDesignationLocation=$sDirectory."/".$dDateTime.$_FILES['flnFile']['name'];

//echo($sDesignationLocation);

move_uploaded_file($sTempLocation,$sDesignationLocation);

}

else

{

echo("File type error");

}

}

else

{

echo("File size Error");

}

?>

What is PHP Delimiter?

Delimiter is a special character or a symbol to seperate the string