Showing posts with label php. Show all posts
Showing posts with label php. Show all posts

Regular Expressions: A Primer

Well, this morning I finally came across a problem where I couldn't avoid regular expressions anymore. Sure, I've used them in form validation, but I've pretty much always just copied existing code for validating email addresses etc, without much attention to the gibberish of characters that magically format or validate my string.

I busted out google and did a bit of searching; this is the page I found to be the most informative in the briefest amount of wordage:

http://www.webcheatsheet.com/php/regular_expressions.php

This page has a nice example chart at the bottom which for me really helped with my understanding of how it all works. I recommend anyone who hasn't bitten the bullet in this department to do a quick read over of this page, as regular expressions are pretty much a staple of any sort of semi-advanced programming.

And yes, you can now mock me for not bothering to learn this prior to now. Yay for self taught programmers!

Initializing an Associative Array in Javascript

Ian presented the following question to me: How do you initialize an associative array with default values in javascript? There is plenty of posts out there on the internet in regards to creating arrays, most look something like:

var myArray = new Array();
myArray["something"] = "some value";


and so on. But what he was looking for was something more like the php method:

$myArray = ( "something"=> "some value", "something else" => "another value");

after a small amount of scouring remembering something about colons, I finally found a scathing post by someone with the revelation: Javascript doesn't actually have associative arrays. In fact javascript doesn't have arrays at all. It has objects. So how do you initialize an associative array in javascript? You don't. You initialize an object:

var arrayTest = {"something":"1st Result","second":"2nd Result"};
alert(arrayTest["second"]);


Hopefully that helps someone out there in Google land.

PHP getimagesize()

Wondering how to find out the size of your image? getimagesize() returns a lovely array containing the height and width of the image, along with a few other bits of data. In fact, one of the array values is even a nice little string containing height="yyy" width="xxx" that can be used directly in an IMG tag.

See the php manual entry for more info on this, as well as a number of other nifty image data gathering functions.

PHP ini_get

A somewhat useful function in PHP - ini_get(string $varname). Useful for troubleshooting, would have come in handy for me once or twice in the past when trying to prove to a server admin that globals were on or off. IE:

echo 'register_globals = ' . ini_get('register_globals') . "\n";

Returns something like:
register_globals = 0
Read the PHP Manual entry for more details.

Sarah Takes the Plunge into OOP. Finally.

Hello non existent blog readers.

*thanking Firefox for spell checking so I don't always spell existent wrong*

This is it. It's time to face the facts. I don't really know anything about Object Oriented Programming - beyond the abstract concepts. And I must. So here goes.

I'm going to be working on it from a PHP angle, as that's the OO language I'm the most comfortable with. I'm going to be starting from here:

http://www.codewalkers.com/c/a/Programming-Basics/Beginning-Object-Oriented-Programming-in-PHP/

Updates to follow of course. Wish me luck.

Sarah Learns a New Term - Namespaces

Working on implementing SWFUpload or a similar type uploader. On reading the documentation, I see:

The main features that SWFUpload provides are:

  • The ability to select multiple files in the file browser dialog.
  • AJAX-style uploading without a page refresh.
  • Upload progress events.
  • Namespaced classes compatible with other JavaScript libraries (i.e., jQuery, Prototype, etc.).
  • Flash 9 and Flash 10 support. (Flash 8 support dropped in version 2.2.0)
This brought me back to the days of my childhood, when, reading a book, I would keep a dictionary beside me and look up unfamiliar words. (Yes I was a big nerd from the moment I was born I guess.) So I decided to look up exactly what a Namespace is and share it with my nonexistent blog readers.

Think about this: You are writing the next big Facebook - your php code is getting to around 10,000 lines, and you are using multiple third party plugins. Suddenly you come across a predicament - that nifty third party User Widget has conflicting class names with that other nifty third party thing-a-majig. Namespacing avoids this by allowing you to 'classify' your code (how punny). Assigning a namespace to a php file (you can even assign more than one per file) ensures that the widget USER class does not get overwritten by the thing-a-majig USER class.

For an explanation that actually makes sense, see here:
http://www.sitepoint.com/blogs/2009/07/13/php-53-namespaces-basics/

Learning Ajax, Json, with php and database manipulation.

I've been struggling today and last week to find some good tutorials or a well recommended book for getting the ball rolling in my quest to learn Ajax. I figure I might as well try to start off working with Json instead of wasting time with parsing string data. So far here are the links I've found useful:

Quick run down on JSON:
Understanding JSON: the 3 minute lesson
JSON on Wikipedia

Excellent Starter tutorial using Ajax (and ASP, but can be easily modified to a php date() function)
W3schools Ajax Example

More Advanced and In Depth Series on Ajax covering numerous topics, which I breezed through the first 3. A much longer read, but contains some really useful details:
Mastering Ajax: 11 Part Series from IBM developerWorks

After you do the W3 Schools Tutorial, I suggest moving on to this one, which uses Ajax, Json, Php, and Database manipulation. Heads up - I had to remove the $$link variable variables from the database functions in order for it to work.
Json Ajax Webchat Tutorial

Good luck, and if you know of some additional resources I would appreciate the links.

PHP in JS?

Seems some clever people are porting php functions into a javascript library. At the very least the md5 function should come in handy. Not sure what the security implications of that are however. Check it out for yourself:

http://phpjs.org/functions/index