ASP to PHP Basics an Overview

Back

The Simple Table below shows the most basic conversion from ASP to PHP Scripting syntax and is very easy to understand.   However; there are No exact conversion for some cases and will include SQL handling soon for both using MySQL.

ASP
PHP
Result
Working with Display


<%
response.write("<h2>You can use HTML tags to format the text!</h2>")
%>


<%
response.write("<p style='color:#0000ff'>This text is styled with the style attribute!</p>")
%>
<?php
echo "<h2> you can use HTML tags to format the text</h2>";
?>

<?php
echo "<p style='color:#0000ff'>This text is styled with the style attribute!</p>";
?>

You can use HTML tags to format the text!

This text is styled with the style attribute!


Reading Parameter from URL


sample URL with parameter to be use as basis:

     http://www.leiron.be/simpleform.asp?fname=Vinson&lname=Tan
<br />
Welcome
<%
response.write(request.querystring("fname"))
response.write(" " & request.querystring("lname"))
%>
<?php
 $fname = @$_GET["fname"];
 
$lname = @$_GET["lname"];

 $fname = (get_magic_quotes_gpc()) ? stripslashes($fname) : $fname;

  $lname = (get_magic_quotes_gpc()) ? stripslashes($lname) : $lname;
?>
<br />
Welcome
<?php echo $fname . " " . $lname; ?>

Welcome Vinson Tan

Working with Session


Creation or Define
<%
Session("username")="Donald Duck"
Session("age")=50
%>

Welcome <%Response.Write(Session("username"))%>
<?php
  session_start();
 
$_SESSION['username'] = "Donald Duck";
 
$_SESSION['age'] = 50;
?>
Welcome
<?php echo $_SESSION['username']; ?>

Welcome Donald Duck


Remove Session Variables

<%
If Session.Contents("age")<18 then
  Session.Contents.Remove("sale")
End If
%>
<?php
session_start();
if ($_SESSION['age'] < 18) {
     unset( $_SESSION['age'] ); }
?>




Remove or Reset ALL Session Variables

<%
Session.Contents.RemoveAll()
%>
<?php
    session_start();
    $_SESSION = array();
?>




Access All Session Values





<%
dim i
dim j

j=Session.Contents.Count

Response.Write("Session variables: " & j)

For i=1 to j
  Response.Write(Session.Contents(i) & "<br />")
Next
%>
<?php
session_start();
if (!is_array($_SESSION['uinfo']))
{
   $_SESSION['uinfo'] = array();
   $_SESSION['uinfo'][0] = "Donald Duck";
   $_SESSION['uinfo'][1] = 50;
}
$j = count($_SESSION['uinfo']);

echo  "Session variables: " .  ($j + 1);

 for ($initcnt=0; $initcnt < $j; $initcnt++) {
    echo $_SESSION['uinfo'][$initcnt] . "<br />";
}
?>

Session variables: 2
Donald Duck
50


Working with Function or Procedures


<%@ language="javascript" %>
....
...
<%
function jsproc(num1,num2)
{
Response.Write(num1*num2)
}
%>
.....
...
<p>Result: <%jsproc(3,4)%></p>

<?php

function jsproc ( $num1, $num2 )
{
    echo ( $num1 * $num2 );
}
?>

<p>Result: <?php jsproc(3,4); ?></p>


Result: 12

Working with Arrays



<%
Dim famname(5),i
famname(0) = "Jan Egil"
famname(1) = "Tove"
famname(2) = "Hege"
famname(3) = "Stale"
famname(4) = "Kai Jim"
famname(5) = "Borge"

For i = 0 to 5
      response.write(famname(i) & "<br />")
Next
%>
<?php

$famname = array(0=>'Jan Egil','Tove','Hege','Stale','Kai Jim','Borge');

for ($cnt=0; $cnt < 5; $cnt++)
{
   echo $
famname[$cnt];
}

?>

Jan Egil
Tove
Hege
Stale
Kai Jim
Borge


Date and Time


Today's date is: <%response.write(date())%>.
<br />
The server's local time is: <%response.write(time())%>.
Today's date is: <?php echo date('m/j/Y'); ?>.
<br />
The server's local time is:
<?php  echo date('h:i:s A');
?>.

Today's date is: 1/27/2010.
The server's local time is: 10:47:50 AM.

Working with FORMS

method = "post"

<%
    dim cars
    cars=Request.Form("cars")
%>
....
...
<input type="radio" name="cars"
<%if cars="Volvo" then Response.Write("checked")%>
value="Volvo">Volvo</input>
<br />
<input type="radio" name="cars"
<%if cars="Saab" then Response.Write("checked")%>
value="Saab">Saab</input>
<br />
<input type="radio" name="cars"
<%if cars="BMW" then Response.Write("checked")%>
value="BMW">BMW</input>
...
...
<%
  if cars<>"" then
     Response.Write("<p>Your favorite car is: " & cars & "</p>")
  end if
%>

...
...
<input type="radio" name="cars" 
  <?php if ($_POST['cars'] == '
Volvo') {  echo  "checked";  }  ?>  value="Volvo">Volvo</input>
<br />
<input type="radio" name="cars" 
 
<?php if ($_POST['cars'] == 'Saab') {  echo  "checked";  }  ?> value="Saab">Saab</input>
<br />
<input type="radio" name="cars" 
 
<?php if ($_POST['cars'] == 'BMW') {  echo  "checked";  }  ?>  value="BMW">BMW</input>
...
...

<?php

  if  (!empty($_POST['cars']) )
  {
      echo "<p> Your favorite car is: " . $_POST['cars'] .
              "</p>";
  }

?>


Please select your favorite car:

Volvo
Saab
BMW




Working with Cookie

Create Cookies

<%
Response.Cookies("firstname")="Alex"
Response.Cookies("firstname").Expires=#May 10,2012#
%>
<?php

   setcookie('firstname','Alex',time()+3600);
?>
     * current time + 1 hour & 60 min.



Retrieve a Cookie value

<%
  fname=Request.Cookies("firstname")
  response.write("Firstname = " & fname)
%>
<?php
  $fname = $_COOKIE['firstname'];
  echo "Firstname = " . $fname;
?>

Firstname = Alex


Working with External Files


<!-- #include virtual ="/html/header.inc" -->
where html is the virtual directory or folder
<!-- #include file ="headers\header.inc" -->

Use the file keyword to indicate a relative path.
A relative path begins with the directory that contains the including file.
?php

   include('../html/headers/header.inc');

?>