Jump to content

Recommended Posts

Hi,

 

Just a quick question. Have found some code using things like:

$this->current_step = (integer) $step;

and wondered what -> means, and what (integer) does as I haven't seen a plain word just written in brackets like that.

 

I've also seen this: ^ used and don't know what it does, and words preceded by an ampersand, like this:

 $installer = &new Installer(realpath('.'));

which I also don't understand. Searched the PHP manual but can't find reference to any of these?

 

Thanks!

Link to comment
https://forums.phpfreaks.com/topic/126951-solved-what-do-these-operators-mean/
Share on other sites

$this->current_step = (integer) $step;

 

The reserved keyword $this refers to the object that is currently being used. As you may or may not know, a class can have any number of objects spring from its loins. Except of course if the Singleton pattern is being used... but that's another story.

 

Basically $step is being forced onto current_step as an integer. If $step is not an integer, (maybe its a string, who knows), it is automatically set to 0.

and the & there is unnecessary since objects are already passed by reference.

 

PHP 5.0 + only.

 

Not in PHP 4, and it was a real PIA with that version. So you could well come across older scripts using th "&"

and the & there is unnecessary since objects are already passed by reference.

 

PHP 5.0 + only.

 

Not in PHP 4, and it was a real PIA with that version. So you could well come across older scripts using th "&"

 

Lol, forgot that not everyone has PHP5 yet.  *grumble*  I remember how bad it was in PHP4.  I didn't even bother with OOP back then, even though I knew how to code in OOP from Java and C++.

Thanks for the explanations, although I'm afraid they went over my head. But, thanks to you putting names to these different things (classes, objects) I've been able to find the relevant parts in the PHP manual - and boy, is there some learning for me to do. I think I got way over my head with this, and now the scary spectre of classes and objects awaits my poor, unprepared brain. Still trying to grasp how to use functions...

Thanks for the explanations, although I'm afraid they went over my head. But, thanks to you putting names to these different things (classes, objects) I've been able to find the relevant parts in the PHP manual - and boy, is there some learning for me to do. I think I got way over my head with this, and now the scary spectre of classes and objects awaits my poor, unprepared brain. Still trying to grasp how to use functions...

 

A lot of people seem to have a lot of trouble grasping functions lately, and I'm not too sure why.  Almost everything you do in a programming language involves functions.  substr(), count(), isset(), etc. are all functions, just not user defined.  Just think about how they're doing their job, and how you invoke them and make them do that job, and then functions should be a lot easier to get.

I've just spent this time reading about functions and practising with them, and I'm finally getting to grips with them to some extent, especially after you pointed out the obvious that things I use like isset() and so on are functions, but not defined by me. Never thought of it like that.

 

Just out of interest, I discovered that when making a function, I had to define a variable to be used with it, like:

function makebold($text) {
$text = "<b>$text</b>";
return($text);
}

echo makebold($text);

 

So $text is used, yet I can write:

$phrase="hello world";
function makebold($phrase);

 

And it will still output correctly. How come it replaces the preset variable $text with any variable I choose? And is there a way of writing functions without having to state a variable (just curious)?

 

The other thing I found was that if I remove return($text); the function no longer works. I don't understand why, since the code for making the text bold is still present.

Just out of interest, I discovered that when making a function, I had to define a variable to be used with it, like:

function makebold($text) {
$text = "<b>$text</b>";
return($text);
}

echo makebold($text);

 

So $text is used, yet I can write:

$phrase="hello world";
function makebold($phrase);

 

And it will still output correctly. How come it replaces the preset variable $text with any variable I choose?

This is because its only the value of the variable that is being passed to the function. Not the variable itself.

 

And is there a way of writing functions without having to state a variable (just curious)?

Yes you can do so by using the global keyword within the function. However this is considered bad programming practice.

 

I would recommend you to read up on variable scope.

 

The other thing I found was that if I remove return($text); the function no longer works. I don't understand why, since the code for making the text bold is still present.

You call the makebold function here:

echo makebold($text);

What will happen here is PHP will run the code in the makebold function first. It'll then echo the value returned from that function.

So If you removed the following return($text); from your function then nothing will be returned, and thus nothing will be echo'd. You see where the problem is now?

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.