code readability question

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

i thought that in c a string is an array of type char the last character of which is a special terminator which is usually written \0.

If that definition is valid, then really don't see the link between string and stack, because the following would allow access to an element at the 4 place.

Code: Select all

#include <stdio.h>
int main(void)
&#123;
    char foo&#1111;] = "abcdefg";
    printf("foo 3: %c", foo&#1111;3]);
    return 0;
&#125;
crouse
Forum Newbie
Posts: 13
Joined: Mon Dec 27, 2004 11:14 pm
Location: Sacramento, CA
Contact:

Post by crouse »

timvw wrote:i thought that in c a string is an array of type char the last character of which is a special terminator which is usually written \0.
In C a string is an array of chars that is null terminated. However, PHP handles strings slightly different. Unlike C, in PHP a string is an actual data type and not just an array characters. In addition to the characters that are stored in a string, PHP attaches the length of the string to the value. I have heard this type of string referred to as a length-prefixed string. Instead of strlen() counting the characters in the string until it hits the end of the line, null terminator, it returns the length value of the string. I am not totally certain if PHP prefixes the length to the string, however I know it is done in other languages and it makes the most sense performance wise. As I was researching this topic I wrote a test using PHP's count() function that returns the number of elements in an array.

Code: Select all

<?php
	$testString = "Test String";
	
	$testArray = array('T', 'e', 's', 't');
	
	print "count of string: " .count($testString) . "<br/>";
	print " count of array: " . count($testArray) . "<br/>";
?>
My main reference for these findings is PHP 5: Power Programming. It is my personal preference to use {} to reference a single character in a string as I have found that it sometimes helps distinguishing between an array and string.

Hope this helps,
Chris
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

OK, i see what you were trying to say...

I already experienced that each position/byte doesn't have to be a character while playing with utf :)
User avatar
carlmcdade
Forum Newbie
Posts: 24
Joined: Thu Dec 02, 2004 2:19 am
Location: sweden

Post by carlmcdade »

Chris,

Thanks for that explaination. It is an important thing to remember. I am trying to get a handle on the Zend Engine extensions written in c and finding the differences between PHP and c to be confounding.


How do you find PHP 5: Power programming to be? I was thinking of buying it but I don't trust Amazon reviews any longer and none of the paper mags have done a review yet.
crouse
Forum Newbie
Posts: 13
Joined: Mon Dec 27, 2004 11:14 pm
Location: Sacramento, CA
Contact:

Post by crouse »

I like the book from what I have read of it. I am not a person to read a computer programming book from cover to cover however. I generally get a book like PHP5: Power Programming as a reference and go to specific sections that interest me or that I think can help me understand a topic better. I have found that the book has a great section on Object Oriented Design and different design patterns for objects.

One I am looking over and attempting to work with is the Singleton class. Basically it is a class that you make the constructor, and clone, private so that others can't create an instance of it. However, the creator of the class provides a public method to get at a private instance of the class. This helps for if you only want one of these items in your application period. An example of this would be like a custom error logger. Sometimes you might want to only have one logger be responsible for all logging activity. Doesn't fit all molds but when it is useful it really is useful.

Chris
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

i can see various places where singleton-pattern can come in handy, and the first time i saw some code implementing it i thought that was sexy :) :)

However, i don't see much use for it in PHP, because it's hard to share objects between different sessions / application - wide.
User avatar
carlmcdade
Forum Newbie
Posts: 24
Joined: Thu Dec 02, 2004 2:19 am
Location: sweden

Post by carlmcdade »

However, i don't see much use for it in PHP, because it's hard to share objects between different sessions / application - wide.
Yes, this is one of the things I miss about ASP is being able to use a global.asa file. If you ask me they should stop all the talk about putting goto and really fix sessions up nicely with a PHP version of the global.asa, application start and some other items.

I myself have been trying to apply the Factory method to Drupal and my own CMS. Which is supposed to make you code really easy to flow and read.

I 'll have to pick up that book along with the older Advanced PHP one as a new years gift to myself :)
Post Reply