Variables in PHP

variables in php 

 declaring variables in php is very simple .its more easier then java and c++ to declare variable

•PHP variables must begin with a “$” sign 

ie $x         , $y

•Case-sensitive 

($Foo != $foo != $fOo) 

•Global and locally-scoped variables 

–Global variables can be used anywhere
–Local variables restricted to a function or class

•Certain variable names reserved by PHP

–Form variables ($_POST, $_GET)
–Server variables ($_SERVER)
–Etc.

•PHP is a loosely typed language

–In PHP, a variable does not need to be declared before adding a value to it
Example code
[code start]

<?php
$foo = 25;  // Numerical variable
$bar = “Hello”;  // String variable
$foo = ($foo * 7);  // Multiplies foo by 7
$bar = ($bar * 7);  // Invalid expression
?>

[code end]

Echo in php 

•The PHP command ‘echo’ 

is used to output the parameters passed to it
–The typical usage for this is to send data to the client’s web-browser

•Syntax

–void echo (string arg1 [, string argn...])
–In practice, arguments are not passed in parentheses since echo is a language construct rather than an actual function 
Example:

[code start]

<html>
<body>
<?php
echo "Hello World";
?>
 <br />
<?php
$txt="Hello World";
echo $txt;
?>
</body>
</html>

[code end]

Example2:
[code start] 
<?php
$foo = 25;  // Numerical variable
$bar = “Hello”;  // String variable
echo $bar;  // Outputs Hello
echo $foo,$bar;  // Outputs 25Hello
echo “5x5=”,$foo;  // Outputs 5x5=25
echo “5x5=$foo”;  // Outputs 5x5=25
echo ‘5x5=$
foo’;  // Outputs 5x5=$foo
?>

[code start] 

•Notice how echo ‘5x5=$foo’ outputs $foo rather than replacing it with 25
•Strings in single quotes (‘ ’) are not interpreted or evaluated by PHP
•This is true for both variables and character escape-sequences (such as “\n” or “\\”)


0 comments:

php vs c/c++ code structure comparison

What does PHP code look like?

What you will learn here.
1.How to escape from HTML and enter PHP mode 
2.Simple HTML Page with PHP 
3.Using conditional statements
4.Comments in PHP



Php code Structurally is similar to C/C++
Php scripting Supports procedural and object-oriented paradigm (to some degree) .
All PHP statements end with a semi-colon Each PHP script must be enclosed in the reserved PHP tag

learn php tutorials syntex



1.How to escape from HTML and enter PHP mode

•PHP parses a file by looking for one of the special tags that
tells it to start interpreting the text as PHP code. The parser then executes all of the code it finds until it runs into a PHP closing tag.
php with html embeded code
<?php echo “Hello World”; ?>

2.Simple HTML Page with PHP 

The following is a basic example to output text using PHP.



[code start]

<html><head>
<title>My First PHP script Page</title>
</head>
<body>
<?php
echo "Hello World!";
?>
</body></html> 

[code end]



Copy the code onto your web server and save it as “test.php”.

You should see “Hello World!” displayed. (if you dont know how to setup localhost/localServer then click here)

Notice that the semicolon is used at the end of each line of PHP code to signify a line break. Like HTML, PHP ignores whitespace
between lines of code. (An HTML equivalent is <BR>)

3.Using conditional statements

•Conditional statements are very useful for displaying specific content to the user. The following example shows how to display content according to the day of the week.  
[code start]
<?php
$today_dayofweek = date(“w”);

if ($today_dayofweek == 4){
echo “Today is Thursday!”;
}
else{
echo “Today is not Thursday.”;
}

?>
 [code end]


The if statement checks the value of $today_dayofweek
(which is the numerical day of the week, 0=Sunday… 6=Saturday)
•If it is equal to 4 (the numeric representation of Thurs.) it will display
everything within the first { } bracket after the “if()”.
•If it is not equal to 4, it will display everything in the second { } bracket
after the “else”. 

If we run the script on a Thursday, we should see:
“Today is Thursday”.

On days other than Thursday, we will see:
“Today is not Thursday.”
[code start]

<?php
$today_dayofweek = date(“w”);

if ($today_dayofweek == 4){
echo “Today is Thursday!”;
}
else{
echo “Today is not Thursday.”;
}

?>

 [code end]


4.Comments in PHP

•Standard C, C++, and shell comment symbols





[code start]

<?php
// C++ and Java-style comment
# Shell-style comments
/* C-style comments
These can span multiple lines */
?>
[code end]

Read next:

Variables in PHP



1 comments:

why php is used? benefits of using php

first read previous article

why php is used? benefits of using php?

using php is beneficial in four ways

  1. Easy to use 

  2. Cross platForm

  3. Cost benefits

Easy to Use

Code is embedded into HTML. The PHP code is enclosed in special start and end tags that allow you to jump into and out of "PHP mode".

<html>
<head>
<title>Example</title>
</head>
<body>

<?php
echo "Hi, I'm a PHP script!";
?>

</body>
</html>

Cross Platform 

Runs on almost any Web server on several operating systems.
One of the strongest features is the wide range of supported databases Web Servers: Apache, Microsoft IIS, Caudium, Netscape Enterprise Server

Operating Systems: 

UNIX (HP-UX,OpenBSD,Solaris,Linux), Mac OSX, Windows NT/98/2000/XP/2003

  Supported Databases: 

Adabas D, dBase,Empress,
FilePro (read-only), Hyperwave,IBM DB2, Informix, Ingres, InterBase, FrontBase, mSQL, Direct MS-SQL, MySQL, ODBC, Oracle (OCI7 and OCI8), Ovrimos, PostgreSQL, SQLite, Solid, Sybase, Velocis,Unix dbm

Cost Benefits

PHP is free. 

Open source code means that the entire PHP community will contribute towards bug fixes. There are several add-on technologies (libraries) for PHP that are also free.

software = free
platform=free
Development Tools=free phpcoder jeditor

0 comments:

what is php history of php and why php is used

what is php history of php and why php is used 

what is php

PHP == ‘Hypertext Preprocessor’
Open-source, server-side scripting language like ASP and JSP
PHP scripts are executed on the server
PHP Used to generate dynamic web-pages
PHP scripts reside between reserved PHP tags
This allows the programmer to embed PHP scripts within HTML pages
PHP supports many databases (MySQL, Informix, Oracle, Sybase, Solid, PostgreSQL, Generic ODBC, etc.)
PHP is free to download and use
Interpreted language, scripts are parsed at run-time rather than compiled beforehand
Source-code not visible by client
‘View Source’ in browsers does not display the PHP code Various
built-in functions allow for fast development
PHP Files

PHP files can 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"


Brief History of PHP

PHP (PHP: Hypertext Preprocessor)

 was created by Rasmus Lerdorf in 1994.
It was initially
developed for HTTP usage logging and server-side form generation in Unix.

PHP 2 (1995) 

transformed the language into a Server-side embedded scripting language.
Added database support, file uploads, variables, arrays, recursive functions, conditionals,
iteration, regular expressions, etc.

PHP 3 (1998) 

added support for ODBC data sources, multiple platform support,
email protocols (SNMP,IMAP), and new parser written by Zeev Suraski and Andi Gutmans

. PHP 4 (2000) 

became an independent component of the web server for added efficiency.
The parser was renamed the Zend Engine. Many security features were added.

PHP 5 (2004)

 adds Zend Engine II with object oriented programming,
robust XML support using the libxml2 library,
SOAP extension for interoperability with Web Services, SQLite has been bundled with PHP


Why we use PHP

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

 Where to Start? 


To get access to a web server with PHP support, you can: Install Apache (or IIS) on your own server, install PHP, and MySQL Or find a web hosting plan with PHP and MySQL support
Easy to Use Code is embedded into HTML. The PHP code is enclosed in special start and end tags that allow you to jump into and out of "PHP mode".   

Easy to Use

Code is embedded into HTML. The PHP code is enclosed in special start and end tags that allow you to jump into and out of "PHP mode".

<html>
<head>
<title>Example</title>
</head>
<body>

<?php
echo "Hi, I'm a PHP script!";
?>

</body>
</html>


Cross Platform 

Runs on almost any Web server on several operating systems.
One of the strongest features is the wide range of supported databases Web Servers: Apache, Microsoft IIS, Caudium, Netscape Enterprise Server

Operating Systems: 

UNIX (HP-UX,OpenBSD,Solaris,Linux), Mac OSX, Windows NT/98/2000/XP/2003

  Supported Databases: 

Adabas D, dBase,Empress,
FilePro (read-only), Hyperwave,IBM DB2, Informix, Ingres, InterBase, FrontBase, mSQL, Direct MS-SQL, MySQL, ODBC, Oracle (OCI7 and OCI8), Ovrimos, PostgreSQL, SQLite, Solid, Sybase, Velocis,Unix dbm

1 comments: