Вы находитесь на странице: 1из 8

PHP Faqs

PHP | AJAX | MySql | OOPS | Pl/SQL

PHP (hyper text Preprocessor) is a computer scripting language. Designed for producing dynamic web pages, with syntax from C, Java and Perl, PHP code is embedded within HTML pages for server side execution it has evolved to include a command line interface capability and can be used in standalone graphical applications. PHP was originally created by Rasmus Lerdorf in 1995; the main implementation of PHP is now produced by The PHP Group and serves as the de facto standard for PHP as there is no formal specification. Released under the PHP License, the Free Software Foundation considers it to be free software. PHP is a widely used general-purpose scripting language that is especially suited for web development, taking PHP code as its input and creating web pages as output. It can be deployed on most web servers and on almost every operating system and platform free of charge. From this you can create more complex loops and functions to make your page generate more specialized data. PHP is installed on more than 25 million websites and 1 million web servers. 1 What is PHP? Ans: PHP stands for:Hyper Text Preprocessor. PHP is a server-side scripting language,like ASP. PHP scripts are executedon the server. PHP supportsmany.databases(MySQL,Informix,Oracle,Sybase,Solid,PostgreSQL,GenericODBC,etc. PHP is an open source software (OSS). 2 What is a PHP File? Ans: PHP files may contain text, HTML tags and scripts. PHP files are returned to the browser as plain HTML. PHP files have a file extension of ".php", ".php3", or ".phtml". 3 How to install PHP on Apache? Ans: You need to add the following lines to httpd.conf (If you're using Apache for Win32). ScriptAlias /php/" c:/path-to-php-dir/" AddType application/x-httpd-php .php Action application/x-httpd-php "/php/php.exe" Then restart Apache and it should interpret php files correctly. 4 Why PHP? Ans: PHP runs on different platforms (Windows, Linux, Unix, etc.). PHP is compatible with almost all servers used today (Apache, IIS, etc.). PHP is FREE to download from the official PHP resource: www.php.net. PHP is easy to learn and runs efficiently on the server side. 5 Are the PHP language constructs (for, return, print...) case sensitive? Ans: NO!! 6 How do I install PHP as a CGI? Ans: What you need to do is to run: ./configure --without-apache --without-apxs --with-other-options.

It will tell you that as you are building it without Apache support, it is building it as a CGI binary. Then do make ; make install and it should then be installed (/usr/local/bin/php by default). 7 How does PHP compare to ASP? Ans: Microsoft's Active Server Pages (ASP) comes with VBScript and JScript scripting languages, but you can also install scripting engines for Perl, REXX, and Python, whereas PHP will only ever be PHP. The most commonly used flavour of ASP is that written in VBScript. VBScript is a subset of Visual Basic, a standalone compiled language. This very fact makes ASP marketable to VB programmers wishing to build applications for the Internet. However, as a script language VBScript could never compete with its big brother, Visual Basic, only supplement it. In making this point, VBScript is not solely a glue to hold Visual Basic together with an Internet front-end, but that's really what it does best. ASP can prove to be very memory hungry beasts and regular calls on system objects, such as Active Data Objects (ADO) for working with databases can make for slow applications on heavily loaded machines. VBScript lacks many important features, which can be added through Component Object Model (COM) objects. An example of this, is when trying to send e-mail using VBScript, although the objects are available, they can be costly and more time consuming than the PHP equivalent (mail function). PHP can also be 'added to' with additional functionality being quite platform dependant. COM functions are supported by the Windows version of PHP and many libraries are available for *nix versions. VBScript interpreters are available for various Unix based systems and Windows. However, finding a COM object that will send e-mail on a Unix system is nearly impossible and very expensive. This leads to the conclusion that ASP is only really at home in a Microsoft environment. It has its fair share of security flaws, but when used by professional VB programmers, I have seen ASP provide some immensely powerful interfaces. PHP on the other hand can be seen to provide the best results in the form of stability and speed on Unix based systems. PHP is Open Source software, which is great as it means that code, manual, updates and support are all free. Although the ASP script engine comes included with IIS and PWS and minimal support is available free, running Microsoft operating systems at a commercial level is always going to be costly. A lack of high profile sites using PHP makes it a bit harder to prove it's robustness and gain acceptance from non-programming colleagues (e.g. management), but for a programmer the portability and stability of PHP is beneficial. 8 What is caching? Ans: "Meta tags are easy to use, but aren't very effective. That's because they're usually only honored by browser caches (which actually read the HTML), not proxy caches (which almost never read the HTML in the document)." If a page is changed "nearly every day", it will hardly be a problem in practical terms.

And in any case, it's something to be handled at the server level, by making the server send some useful expiration information, using whatever needs to be done on a specific server. Telling that a page expired twenty years ago is hardly a good idea if you can expect its lifetime to be a day or more, or at least several hours. Defeating proxy caching brutally wouldn't be a good idea (and meta tags won't do that, so the errors in a sense cancel out each other, so to say This has to be at the beginning of the file, with nothing before (e.g. no blank). This is a brute force variation, some adjustments are useful. (Server supporting PHP is recommended)Meta-tags wont work with proxies. Proxies don't work on the 'HTML-layer' but HTTP. Things depend on proxy settings also."The Pragma header is generally ineffective because its meaning is not standardized and few caches honor it. Using <meta http-equiv=...> elements in HTML documents is also generally ineffective; some browsers may honor such markup, but other caches ignore it completely." - Web Design Group That's because the no-cache pragma is supposed to be part of a HTTP *request*. And *this* has been standardized since way back. 9 Are the PHP variable names case sensitive? Ans: Yes. The php variable names cases sensitive. 10 Are the PHP function names case sensitive? Ans: No, Its not case sensitve functions. 11 What are the differences between PHP 3 and PHP 4? Ans: Here's a list of some of the more important new features: >> Extended API module. >> Generalized build process under Unix. >> Generic web server interface that also supports multi-threaded web servers Improved syntax highlighter. >> Native HTTP session support. >> Output buffering support. >> More powerful configuration system. >> Reference counting. 12 Can I use php with cron on unix systems? Ans: Yes that's possible, how you do it depends a little on your installation. 1. If you have php installed as CGI on your system you can ex. simply do: 0 10 * * * /path/to/php /path/to/script > /dev/null. 2. If you dont have php installed as CGI you can use a unix console program for fetching URLs, we reccommend lynx or wget. Example of using lynx: 0 10 * * * lynx -dump http://fsck.dk/script.php > /dev/null Example of using wget:

0 10 * * * wget http://fsck.dk/script.php > /dev/null..

13 How do I get the visitors browser information? Ans: You have more than one option when you want to get the information, they depends on how much information you want to get bout the browser. We will mention 2 ways here. The first way to do it is simply by using the variable $HTTP_USER_AGENT, which holds information about browser version, name etc. If you want a class that can parse the information out of the variable look at the PHP Client Sniffer. 14 What is MySQL? Ans: MySQL is a database server. MySQL is ideal for both small and large applications. MySQL supports standard SQL. MySQL compiles on a number of platforms. MySQL is free to download and use. 15 Where to Start? Ans: Install an Apache server on a Windows or Linux machine. Install PHP on a Windows or Linux machine. Install MySQL on a Windows or Linux machine. 16 How can I get PWS (Microsoft Personal Webserver) to work? Ans: A lot of people have problems with PWS. One answer might be the following (other suggestions welcome - email from the contact page if you want to suggest something). 17 What is CGI? Ans: CGI: Common gateway interface Allows a web server to actually run a program and return the output as the "web page". Very flexible and very useful, especially when combined with Perl or mod_perl, but can be used with C, bash, Python or a number of other languages. (anything the server can run). CGI is an older technology, and has since been superceded by the following technologies: mod-perl. PHP. JSP and servlets. Server side. Javascript. NSAPI and IISAPI. ASP. J2EE Web Services.

.net web services. Although superceeded, CGI is still widely available and used,essentially.because of its ease of setup and the flexibility on which programming language is used. 18 What is the relation between the versions? Ans: PHP/FI 2.0 is an early and no longer supported version of PHP. PHP 3 is the successor to PHP/FI 2.0 and is a lot nicer. PHP 4 is the current generation of PHP, which uses the Zend engine under the hood. PHP 5 uses the Zend engine 2 which, among other things, offers many additional OOP features. 19 How do I store binary data in mysql ? Ans: We sugest you read the 2 following articles in order to learn the best techniques to insert and retrive binary data from databases (mostly focused at mysql). Data Goes In, Data Comes Out This Time it's Binary Binary Data + MySQL + PHP. 20 What is the difference between mysql_connect() and mysql_pconnect()? Ans: There is a good page in the php manual on the subject, in short mysql_pconnect() makes a persistent connection to the database which means a SQL link that do not close when the execution of your script ends. 21 How do I limit the number of rows I get out of my database? Ans: Use LIMIT in your SQL query like this: SELECT name FROM table LIMIT 10 if you want to get the rows between 10 and 20 do the following: SELECT name FROM table LIMIT 9, 10..

22 Can I get the screen resolution and like with php? Ans: The quick answer is no, then you are probably asking why can't I do that with php. OK here is a longer answer. PHP is a serverside scripting language and therefor has nothing to do with the type of a specific client. Then you might ask why can I then get the browser agent from php?", thats because that information is sent with the initial HTTP headers upon requst to the server. 24 How can I set the time zone for MySQL to UK time? Ans: If you set the environment variable TZ=GB, then start MySQL the time zone will be set to UK time. I.E. (Linux only). bash:#TZ=GB. bash:#export TZ.

bash:#mysqld &. You can also set environment variables in my.cnf; In the data (normally var) directory. How can I set the include path and other settings that are fixed in php.ini without 25 having access to php.ini? Ans: If you are hosting on a Linux server you may (Depending on your host) be able to use .htaccess this file must be in the root of the web directory.The format is along the lines of (For PHP4 Module):php_value include_path .:/path/that/you/want/to/use php_value magic_quotes_gpc "off" php_value track_errors "on." 26 How can I convert a Access database to a MySQL database? Ans: The easy way and the way we recommend to do it is to download mysqlfront which has the capability of importing ODBC sources to mysql databases. It is also possible to convert your access database to csv files and then import them into your mysql database structure (ex with LOAD FILE). 27 How do I run a php script from the command line? Ans: No matter what OS you have installed you need to compile / install php as a CGI because when you have php as a CGI you have a binary you can execute from anywhere like a command prompt. If you use a unix like OS you should do:/path/to/php -/path/to/script.phpForwindowsOS's do: c:\path\to\php.exe -f c:\path\to\script.php. The parameter -f is put on because we want to parse a file, the -f also implies -q which means you dont get any HTTP header output which in most cases is usefull when making command line scripts. 28 Whats the difference between include() and require()? Ans: Qouted from the php manual: Unlike include(), require() will always read in the target file, even if the line it's on never executes. If you want to conditionally include a file, use include(). The conditional statement won't affect the require(). However, if the line on which the require() occurs is not executed, neither will any of the code in the target file be executed. In PHP 3, it is possible to execute a return statement inside a require()ed file, as long as that statement occurs in the global scope of the require()ed file. It may not occur within any block (meaning inside braces ({}). In PHP 4, however, this ability has been discontinued. If you need this functionality, see include(). 29 What is PHP relation to PHP/FI? Ans: PHP is the successor to PHP/FI 2.0 30 Can we run both PHP/FI 2.0 and PHP at the same time?

Ans: Yes, PHP was written so as to not interfere with an existing PHP/FI 2 installation. Instructions for building Apache 1.3.0 with both PHP/FI 2 and PHP modules can be found. although the first step where the PHP2 regex code is substituted with the PHP regex code seems redundant. The two regex dirs are pretty much identical 31 What is the differences between PHP and PHP/FI 2.0? Ans: All-new parser.

Persistent database connections. A native Windows95/NT port.

IMAP, SNMP, and LDAP extensions. 32 Where can we obtain PHP Code? Ans: We can download PHP from any of the members of the PHP network of sites. We can also use anonymous CVS to get the absolute latest version of the source 33 What is the difference between PHP and JavaScript? Ans: javascript is a client side scripting language, so javascript can make popups and other things happens on someones PC. While PHP is server side scripting language so it does every stuff with the server.

34 How do we read a file using php? Ans: The following are basic steps followed in reading a file using PHP Open a file using fopen lock the file using flock read the contents of the file using fread unlock the file using flock close the file using fclose PHPcode: <?php $filename = "../example/file.txt"; $handle = fopen($filename, "r"); flock($handle,LOCK_SH); $contents = fread($handle, filesize($filename)); echo $contents; flock($handle,LOCK_UN); fclose($handle); ?> Step:1

$filename = "../example/file.txt"; The above line of the code specifies name of the file. Step:2 $handle = fopen($filename, "r"); By using fopen we can open the file and r is for read mode. If the file doesn't exists, it will show you an error Step:3 flock($handle,LOCK_SH); flock is used for LOCK the file .By using an shared lock,(which is set by putting LOCK_SH.). When a statement reads data without making any modifications, its transaction obtains a shared lock on the data. Step:4 $contents = fread($handle, filesize($filename)); By using fread we can read the data from the file. Read the file until end of the file will be reached. step:5 flock($handle,LOCK_UN); We can unlock the file by using flock ( LOCK_ UN ) step:6 fclose($handle); We can close the file by using fclose