Variables in PHP part 1

Posted on January 6th, 2009 in PHP by admin

A variable is an area of memory which is set aside to store information , this is assigned an identifier by the programmer . You can recognise variables in PHP because they are prefixed with the dollar ( $ ) sign . To assign a variable you use the assignment operator ( = ) . Here is an example of this.

$name = “shedboy”;
$intDaysInWeek = 7;

In the first example the variable identifier in this example is $name and the string value “”iain hendry”" has been assigned to it . In the second example the variable identifier is $intDaysInWeek and the number 7 is assigned to it . Note that in the number example we do not surround the variable with quotes in this way PHP treats this as a numeric value but if we had put quotes round it then PHP would have treated it as a string.

Variable Naming

There are some guidelines to follow for naming variables in PHP and these are as follows .

1. All variable names must begin with a letter or underscore character .

2 . The name of the variable can be made up of numbers , letters , underscores but you cant use charcters like + , - , & , £ , * , etc

One important thing to note if you are coming from another programming language there is no size limit for variables .

Case Sensitivity

One thing that causes many hours of hair pulling and anguish is case sensitivity , PHP is case sensitive (some languages are not) . Here is an example of what I mean.

<?php
$myname = “shedboy”;
echo $Myname”";
?>

Now we all know what we want to do , declare a variable , assign it the value of “shedboy” and then print this on the screen but in this example we have mis-spelt the variable name , when run the following error is displayed on the screen.

Warning: Undefined variable: Myname in D:\testsample.php on line 3

Example

Using your favourite HTML editor enter the following

<?php
$url = “http://www.programmingsite.co.uk”;
$rank = 1;
echo “Our favourite site is ” . $url;
echo “<br>”;
echo “It is number “.$rank;
?>

here is the output you should get

Our favourite site is http://www.myscripting.com

It is number 1

In part 2 we will look at Data types , Type casting

Windows API example in Visual Basic

Posted on January 6th, 2009 in Visual Basic by admin

Displaying the total memory on the system.
This simple API example we are going to get the total memory available on the system.
Start up Visual Basic as usual and enter the code below in the general declarations section of the form.

Option Explicit

Private Type MEMORYSTATUS

dwLength As Long
dwMemoryLoad As Long
dwTotalPhys As Long
dwAvailPhys As Long
dwTotalPageFile As Long
dwAvailPageFile As Long
dwTotalVirtual As Long
dwAvailVirtual As Long

End Type

Private Declare Sub GlobalMemoryStatus Lib “kernel32″ (lpBuffer As MEMORYSTATUS)

Now enter the following in the Form_Load event procedure

Private Sub Form_Load()

Dim memStat As MEMORYSTATUS
GlobalMemoryStatus memStat

MsgBox “Total memory : ” & memStat.dwTotalPhys / 1024 / 1024 & “megabytes”

End Sub

You should now have a layout like this

Now run the program (Press F5) and all going well you should see the total memory of your system. Here is what was displayed

on the test system.

Analysis

The first thing is a user defined type MEMORYSTATUS, this contains eight members all of which are Longs. The member we are interested in is dwTotalPhys.

Next we insert the GlobalMemoryStatus API function which is used to retrieve the memory information.

Now in the Form_Load event we declare various code, the first snippet declares a variable memStat of type MEMORYSTATUS. The MEMSTAT variable is passed to the GlobalMemoryStatus API function which fills in the information in to the MEMORYSTATUS user defined type.

We then display the memory size in a msgbox . The result is in bytes so we divide it twice by 1024 to first convert to kilobytes and then megabytes, you could of course do this again if you have lots of memory in your system.

Further Projects

Display all of the MEMORYSTATUS members on a form.
Build a resource meter which shows features such as memory load and page file usage.

send email with ASP

Posted on January 6th, 2009 in ASP by admin

Sending Email with ASP

All good websites need to be able to send and recieve email . Ok you could have a mailto link in your page pointing to your email address which when the user clicked on it their default email client opened up but why not let ASP handle this . We will use ASP and CDONTS for this task .

CDONTS is available on the Windows NT /2000 platforms and provides the necessary objects , methods and properties to send email . You can also send attachments , send HTML mail and various other tasks .

Lets Begin :

First of all we have to create an instance of the CDONTS object , we achieve this like this

<%
Dim objCdonts
Set objCdonts = Server.CreateObject(”CDONTS.NewMail”)
%>

Now we add the various importantemail addresses  to the script , these include the address you are sending to , the address where the mail is from .

objCdonts.To = “youraddress@youremail.com”
objCdonts.From = “myaddress@myemail.com”

Now lets add the subject line of the email message. We do this with the following.

objCdonts.Subject = “Hello this is my sample email”

OK hopefully you are still with me , an email is no use without some message in the actual body of it , here is how we go about this .

objCdonts.Body = “Hello , I am just testing this email script out “

OK that’s our email all set up and ready to go but wait we need to send it and once it is sent it always good practice to delete the instance of the CDONTS object and free up resources on your server.

objCdonts.Send
Set objCdonts = Nothing

Lets see the complete script

<%
Dim objCdonts
Set objCdonts = Server.CreateObject(”CDONTS.NewMail”)
objCdonts.To = “youraddress@youremail.com”
objCdonts.From = “myaddress@myemail.com”
objCdonts.Subject = “Hello this is my sample email”
objCdonts.Body = “Hello , I am just testing this email script out ”
objCdonts.Send
Set objCdonts = Nothing
%>

Date and Time

Posted on January 6th, 2009 in ASP by admin

Date and Time

Knowing how to use the various date and time functions is very important for all ASP programmers , in this tutorial we will show you the basic date and time functions.

If you want to get the current date you can use the Date function .

<%
Response.Write “The date is ” & Date
%>

which will give us the following

<% Response.Write “The date is ” & Date %>

We can also display the current date and time using the Now function .

<%
Response.Write “The current date/time is ” & Now
%>

which will display the following

<% Response.Write “The current date/time is ” & Now %>

We can display the year , month and day by using the functions Year , Month and Day .

<%
Response.Write “The year is ” & Year(Date) & “<br>”
Response.Write “The month is ” & Month(Date) & “<br>”
Response.Write “The day is ” & Day(Date) & “<br>”
%>

which will display the following

<% Dim theDate theDate = Date Response.Write “The year is ” & Year(Date) & ”
” Response.Write “The month is ” & Month(Date)& ”
” Response.Write “The day is ” & Day(Date)& ”
” %>

We can also get the hours , minutes and seconds by using the Hour , Minute and Second functions

<%
Response.Write “The hour is ” & Hour(Now) & “<br>”
Response.Write “The minutes are ” & Minute(Now) & “<br>”
Response.Write “The seconds are ” & Second(Now) & “<br>”
%>

which displays the following

<% Response.Write “The hour is ” & Hour(Now) & ”
” Response.Write “The minutes are ” & Minute(Now) & ”
” Response.Write “The seconds are ” & Second(Now) & ”
” %>