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 PHP statement outputs text on the screen?

There are several different ways to output text on the screen in PHP two of the most common are echo and print.

echo "Hello World!";

print ("Hello World!");

Would both print ...

Hello World!

How do you create standalone applications with PHP?

There are currently two ways of developing stand alone PHP applications. Both options are only available for Windows platforms (XP and Greater).

PHP-GTK is a development that utilizes the GTK visual interface platform and allows developers to create glade interfaces.

WinBinder is another development that binds PHP natively into the Windows platform.

How can you check if a variable is a string or an integer in PHP?

Integers - The "is_int()" function can be used to check if a variable is has an integer for its value. ---- is_int($variable); // Returns true if $variable is an integer - otherwise false ---- Numeric Strings - Numeric strings are strings with numbers (or things that count as numbers) in them. Numeric-string variables are not integer variables. Numeric-string variables are passed on through forms, instead of integer variables - if you were wondering. Check form values using string formats, and not integer formats. The "is_numeric()" function can be used to check if a variable is a string with numbers - and only numbers - in it (except things that add up to be numbers). ---- is_numeric($variable); // Returns true if $variable is a string, and only contains numbers (broadly speaking) - false otherwise ---- Strings - String values are just text, basically. String variables can contain integers, but that does not make it an integer-type variable - it makes it a numeric string variable. The "is_string" function can be used to check if a variable contains the value of a string. ---- is_string($variable); // Returns true if $variable is a string - false otherwise

How do you declare a constant using PHP?

Outside of a class definition, constants can be declared with the define function. The function has two arguments; the name of the constant (a string), and the value of the constant (any variable type).

Inside a class definition, constants can be declared with the const keyword. Type the word const, the name of the constant in the form of how it will be called later, an "equals sign," then the desired value of the constant.

In both cases, here is an example.

// Outside of a class definition

define('foo', 'Value!');

echo foo; // Prints "Value!"

// Inside a class definition

class example {

const bar = "Value!";

}

echo example::bar; // Prints "Value!"

?>

You use a Html button in Php on its onclick you made a function but its not responding?

this is not really a question... But I think you are asking why php functions inside a HTML button do not work.

Well probably because php is rendered serverside and is passed to a user-browser afterwards.... it is possible to use php inside onclick... but only to display specific content (javascript function for instance)... it is not possible to let a user decide to run a specific php function by clicking a button.

How can you check whether your PHP installation has got IMAP support?

It's probably in the php.ini file, you can also have a look at the current configuration of your PHP by running a file on the webserver with the following code:

<?php

phpinfo();

?>

goodluck

What is the difference between PHP and CGI?

The benefits of running PHP-CGI are:

*

* It is more secure. The PHP runs as your user rather than dhapache. That means you can put your database passwords in a file readable only by you and your php scripts can still access it!

* It is more flexible. Because of security concerns when running PHP as an Apache module (which means it runs as our dhapache user), we have disabled a number of commands with the non-CGI PHP. This will cause installation problems with certain popular PHP scripts (such as Gallery) if you choose to run PHP not as a CGI!

* It's just as fast as running PHP as an Apache module, and we include more default libraries.

There are a FEW VERY MINOR drawbacks to running PHP-CGI. They are:

*

* Custom 404 pages won't work for .php files with PHP-CGI. Or will they? See n74's comment below!

* Variables in the URL which are not regular ?foo=bar variables won't work without using mod_rewrite (http://httpd.apache.org/docs/mod/mod_rewrite.html) (example.com/blah.php/username/info/variable).

* Custom php directives in .htaccess files (php_include_dir /home/user;/home/user/example_dir) won't work.

* The $_SERVER['SCRIPT_NAME'] variable will return the php.cgi binary rather than the name of your script

* Persistant database connections will not work. PHP's mysql_pconnect() function will just open a new connection because it can't find a persistant one.

If one of those is a show-stopper for you, you can easily switch to running PHP as an Apache module and not CGI, but be prepared for a bunch of potential security and ease-of-use issues! If you don't know what any of these drawbacks mean, you're fine just using the default setting of PHP-CGI and not worrying about anything!

Explain the procedure to be followed in post decompression?

1.EMERGENCY DECOMPRESSION Emergency decompression is a rapid loss in cabin pressure. It is unlikely that an involuntary loss of pressurization will occur, however, a decompression may follow some failure of the fuselage such as cracked window panes, malfunction to the aircraft pressurization system, window or door pressure leak or structural damage to the fuselage. Physiological Symptoms of a Decompression · Headache · Respiratory changes and difficulties · Excessive Sleepiness · Light headed or dizzy sensations · Blue coloring of skin, lips, fingernails · Indifference and a feeling of well-being · Fatigue · Deterioration of the senses · Personality changes · Unconsciousness Procedures to Follow During an Emergency Decompression · All Flight Attendants should immediately take oxygen from the nearest 02 mask and secure themselves. · While proceeding to the nearest available mask, give commands "USE OXYGEN MASK", "NO SMOKING", "FASTEN SEAT BELT" · Remain seated until advised by the captain that oxygen is no longer required Turn cabin lights to bright. · Check and assist passengers. · Administer First Aid Oxygen as required · Do not repack aircraft oxygen masks after the masks have dropped. Oxygen masks must be repacked by qualified personnel · Customers should place the mask over their nose and mouth and breathe normally. They should continue wearing the mask until advised by the crew. · Once oxygen from the cabin emergency system is no longer required, customers requiring additional oxygen will be administered first aid from a POB. · Any time the 02 masks deploy, maintenance must repack them NOTE: Generation of the individual chemical units may cause a "burning" odor which may be easily mistaken for odors from a fire. In addition, the chemical canisters can become very hot. Caution should be taken to avoid touching any chemical generator.

How do you use cookies in PHP?

You can set a cookie using the "setcookie()" function, which takes the following arguments:

setcookie($name, $value, $lifetime, $path, $domain, $secure, $httponly);

$name = The name of the cookie (String)

$value = (Optional) The value of the cookie, like a username or code (String)

$lifetime = (Optional) The lifetime of the cookie (Integer of seconds)

$path = (Optional) The directory / path of a server, in which the cookie will be available in (String)

$domain = (Optional) The domain name at which the cookie is available at (String)

$secure = (Optional) Determines whether the cookie should only be sent while there is a secure connection (If you don't know what this is, just make it false) (Boolean)

$httponly = (Optional) Determines whether the cookie can only be accessed through the HTTP protocol, and not by other scripting languages like JavaScript or VBScript (Boolean)

The only arguments you need to focus on here, for starters, is the $name, $value, and $lifetime. All other arguments are optional and can be left alone, just for the purpose of learning how to set a cookie.

Here is an example of how you could set up a cookie:

Setting a cookie


setcookie('mycookie', 'hello', time()+3600);

We set the name of the cookie to "mycookie".

We said the value of the cookie is "hello".

We said that the cookie should expire at the current time + 3,600 seconds. This means that the cookie should delete itself after 3,600 seconds.

Displaying a cookie's value

Now that you have set the cookie, your going to need to retrieve it. When you send a cookie, it won't be available on the same page - you must go to another page, or refresh the page, to have the cookie truly "set".

The $_COOKIE variable contains all the values of every cookie, stored on a user's computer, that is available to the website.

You can access a specific cookie by putting it's name into the $_COOKIE array. For example, in our case, we would say $_COOKIE['mycookie'] to retrieve our cookie information.

echo $_COOKIE['mycookie']; // Displays "hello"

Changing the value of a cookie

You can also alter the value of a cookie by "setting" the cookie again, but with a different value.

setcookie('mycookie', 'its a different value!', time()+3600);

Deleting a cookie

And, of course, you may want to delete cookies. There isn't a specific delete-cookie function. However, you can "set" the cookie again, and specify a time in the past - so then the browser should immediately dispense of the cookie.

You won't need a value, since it's going to delete itself anyway. You can go ahead and set it to the NULL constant (or, give it a value - either way works).

setcookie('mycookie', NULL, time()-3600);

// or

setcookie('mycookie', 'goodbye', time()-3600);

// or

setcookie('mycookie');

For more information about cookies, see the PHP and w3schools references, respectively listed.

[ http://www.php.net/setcookie ]

[ http://www.w3schools.com/PHP/php_cookies.asp ]

What is the difference between an excerpt and a quote?

a quote is a statement that has been made by someone else. en excerpt is a passage or extract (meaning more than one line) from a larger work...such as a newspaper or book/novel.

think of a quote as being "this is what they said" and an excerpt as being "this is why they said it/ what they mean" to explain the qoute.

an excerpt and a qoute can both explain a certain subject further by expressing a sismilar or opposite point of view.

What is the date of manufacture and current value of a Crescent Firearms Victor Special 410 with serial 4255?

Somewhere around 1920. That should be an internal-hammer sidelock shotgun with casehardened frame and locks, rust blued barrel, and varnished wood with moderate checkering. Crescent shotguns, generally speaking, are only just now emerging into the collectable field. Many, many thousands of shotguns were produced by Crescent under many different trade names from the late 1800's through 1930, when they became merged with Davis and eventually absorbed by Savage. During that time they were a wholly-owned part of H&D Folsom, the largest firearm distributor to hardware and catalog stores in the country. Over a hundred-fifty brand names can be attributed to Crescent, with 'Victor Special' being one of the many. A few oddballs (like me) enjoy collecting the hardware store/Crescent shotguns because there is still so much to discover. The good news is that the smaller bores are downright scarce, and a .410-bore Crescent double in excellent condition will bring more than $500 all day long and even get close to a grand in the right company. Value drops off fast, though, with wear and tear. 'Beater' guns wont break $350, even in .410-bore. sales@countrygunsmith.net

How do you use CSS file in php?

<head>

<title>John Deere decks by crigby</title>

<meta http-equiv="content-type" content="text/html; charset=UTF-8"/>

<!-- <base href="opebycrigby.tripod.com"/> -->

<meta name="keywords" content="Outdoor power equipment, graphics of John Deere deck belt routing"/>

<meta name="description" content="graphics of belt routing on decks of John Deere riding mowers"/>

<meta name="author" content="Clarke Rigby"/>

<meta name="copyright" content="&copy; 2009 Clarke Rigby"/>

<link href="../graphics.css" type="text/css" rel="stylesheet" media="screen"/>

<link href="../print.css" type="text/css" rel="stylesheet" media="print"/>

</head>

This is the Head element of one of my pages. Notice that I use one stylesheet for display and one for printing should a visitor decide to do that.

How are string variables stored?

Typically as null-terminated character arrays. However, some languages use the first element of the array to store the length of the string rather than a null-terminator to mark the end of the string.

What is a regular expression?

In programming, a regular expression is an expression that explains a pattern for a string. A string matches a regular expression if that string follows the pattern of that regular expression. For example, you may want to create an account system, allowing usernames to only have uppercase and lowercase letters, and numbers. While a user is registering, you can check their desired username against a regular expression involving only alphanumeric characters (A-Z, a-z, 0-9). If it matches, then the username is valid to your requests. If it does not, the user has put in a character that does not follow the pattern of the regular expression. In regular expressions, you can match certain characters, match a certain quanity of characters, match the casing of characters (or just ignore it overall), and plenty more. The syntax of a regular expression varies throughout every programming language, but Perl is often used due to its wide variety of options. Perl is also incorporated into many web languages, such as PHP, making regular expressions less of a hassle. This is an example of a Perl regular expression, allowing a string with only alphanumeric characters (any character case), and an infinite length (except for a string with no length or no characters, in which the regular expression does not match the string): /^(?i)[a-z0-9]+$/

Write a java script program to print first ten odd natural numbers in C?

Q.1 Write a program to print first ten odd natural numbers. Q.2 Write a program to input a number. Print their table. Q.3 Write a function to print a factorial value.

What is memory leak?

When a piece of software running for an extended time uses more and more memory. The software is not releasing its resources correctly! And so the machine cannot recoup memory.

It is possible that eventually the machine will crash, or the offending software will need restarted.

Is PHP a compiler or interpreter?

It is normally interpreted by another computer program. However subsets of the language can be compiled.

What arguments can be made against the idea of a single language for all programming domains?

You cannot. The only programming language understood natively by a machine is its own machine code. Every architecture has its own variant of machine code and for good reason. Just as the machine code for a piano player would make little or no sense to a Jacquard loom, the machine code for a mainframe would be impractical for a smart phone. Each machine has a specific purpose and therefore has its own unique set of opcodes to suit that purpose. Although some of those opcodes will be very similar and may have the same value associated with them, they won't necessarily operate in exactly the same way, so the sequence of opcodes is just as important as the opcodes themselves. Thus every machine not only has its own machine code it also has its own low-level assembly language to produce that machine code.

We could argue that we only need one high-level language, of course, but then that one language would have to be suitable for all types of programming on all types of machine. This is quite simply impossible, because some languages are better suited to certain domains than others. For instance, Java is an incredibly useful language because it is highly portable, but it is only useful for writing application software. It is of no practical use when it comes to writing operating system kernels or low-level drivers because all Java code is written against a common but ultimately non-existent virtual machine. If it were possible to write an operating system in Java, the extra level of abstraction required to convert the Java byte code to native machine code would result in far from optimal performance; never mind the fact you need to an interpreter to perform the conversion in the first place.


C++ is arguably more powerful than Java because it is general purpose and has zero overhead. Other than assembly, there is no other language capable of producing more efficient machine code than C++. However, C++ isn't a practical language for coding artificial intelligence systems; for that we need a language that is capable of rewriting its own source code, learning and adapting itself to new information. C++ is too low-level for that.


The mere fact we have so many high-level languages is testament to the fact we cannot have a single language across all programming domains. Languages are evolving all the time, borrowing ideas from each other. If a domain requires multiple paradigms for which no single language can accommodate, we can easily interoperate between the languages that provide the specific paradigms we need, possibly creating an entirely new language in the process. That's precisely how languages have evolved into the languages we see today.

How can you submit a form without using a submit button in PHP?

Using Javascript.

This method will use a function attached to a Button but you can call this at any point after the Form has loaded.

What is the difference between PHP and HTML?

PHP is used with HTML to interact with the web server to process forms, access and manipulate database information and a variety of other processes. HTML alone cannot interact directly with the web server. HTML is primarily used to tell the browser where to place text and images to create the web page. PHP interacts directly with the server to process data and display it on the web page using HTML. You can display a web page with only HTML, but if you have any data in a database on your server, for instance, you must also use PHP (or a similar programming language like ASP, Perl, etc.) to access that data.

Quite a few websites, if not a majority, only need HTML to display their information. PHP is definitely not required in many cases. HTML, or an XML variation, is alway required to display a web page.

What are the various ways to embedded PHP in HTML coding?

there are in fact two basic methods - one is called minimal PHP, and the second tends to be referred to as the CGI-way.

minimal php means that you're making php secions in HTML only where really necessary (using the php section begin (<?php ) and end (?> ) tags.).

the CGI-way, or maximum PHP means, that you're in fact embedding HTML into strings in php, and the whole page is echo()ed.

the second way tends to be viewed as an abuse of php by some people, as php was meant to be "templating" language, and designed to be used mainly the first way.

Why is PHP considered a scripting language and not a programming language?

PHP is a programming language, just like Perl, Python, JavaScript, Basic and many other interpreted language.

Note: the PHP interpreter can be integrated into the webserver, or run standalone.

What are the advantages of PHP over Java?

HTML - displays data. That's it. That's all it can and will ever do.

as for PHP, I'll list a few things that it's capable of:

-database interaction (add, modify, delete data. Alter database structures and more)

-Output dynamic contents. (ie, do different things according to the time of day, number of time the user has logged in, number of files in a directory, entries in database, etc.). And it's not just text and numbers either. It can generated images, output PDF and more~

-string/text/date manipulation

-error checks

-sessions and cookies (where website remembers you for a period of time)

-compressions and archives

-Cryptography extensions

-maths

and MUCH, MUCH more.

Advantages of binary search over sequencial search?

Linear search takes linear time with a worst case of O(n) for n items, and an average of O(n/2). Binary search takes logarithmic time, with a worst and average case of O(n log n). Binary search is therefore faster on average.

How do you check given string is palindrome or not with out using string functions?

/*To check whether a string is palindrome*/

  1. include
  2. include
void main () {
int i,j,f=0;
char a[10];
clrscr ();
gets(a);
for (i=0;a[i]!='\0';i++)
{
}
i--;
for (j=0;a[j]!='\0';j++,i--)
{
if (a[i]!=a[j])
f=1;
}
if (f==0)
printf("string is palindrome");
else printf("string is not palindrome");
getch ();
}