Welcome! Log In Create A New Profile

Advanced

PHP 8 - Functions

Posted by 77843835AlexB 
Announcements Last Post
Announcement SoC Curricula 09/30/2017 01:08PM
Announcement Demarcation or scoping of examinations and assessment 02/13/2017 07:59AM
Announcement School of Computing Short Learning Programmes 11/24/2014 08:37AM
Announcement Unisa contact information 07/28/2011 01:28PM
avatar PHP 8 - Functions
February 25, 2013 10:25AM
Still trying to get my head around this. The & in Functions is a little difficult to understand. In this code

Language: PHP
<?PHP   $Variable_Value = 10;   print "Before the function call = " . $Variable_Value . "<BR>";   example($Variable_Value);   print "After the function call = " . $Variable_Value;   function example($Variable_Value) { $Variable_Value = $Variable_Value + 10; print "Inside of the function = " . $Variable_Value . "<BR>"; }   ?>

Here you get the output of

Before the function call = 10
Inside of the function = 20
After the function call = 10

I would have thought that if you return a value out of the function it will also give you the output of:

Before the function call = 10
Inside the function call = 20
After the function call = 20

But:
Language: PHP
function example(&$Variable_Value)

does not make sense. confused smiley You now change your original variable value. What if the original variable value is needed again somewhere else in you program is there a way to revert or would one just not use the & in Functions?? Would this make changes to variables that have been declared as constants??
avatar
Mac
Re: PHP 8 - Functions
February 25, 2013 11:44AM
The value inside the function stays inside the function and is not available outside the function. To make it available outside the function you need to declare it as a global.

Language: PHP
function example($Variable_Value) {   global $Variable_Value;   $Variable_Value = $Variable_Value + 10; print "Inside of the function = " . $Variable_Value . "<BR>"; }


Now it will be:

Before the function call = 10
Inside of the function = 20
After the function call = 20
avatar Re: PHP 8 - Functions
February 25, 2013 12:04PM
Ahhh!! Now that makes more sense. Thanks Mac!!

Is PHP the same as other programming languages where one should keep your Global variables to a minimum?
avatar
Mac
Re: PHP 8 - Functions
February 25, 2013 12:18PM
Do not get confused - there is a difference between making a variable global outside a function, and global variables. For the latter see http://onlamp.com/pub/a/php/2003/03/20/php_security.html
(Do Not Trust Global Variables)
Re: PHP 8 - Functions
February 25, 2013 12:31PM
This was a tricky section but it's done...for now. I did find the include part interesting though.
Re: PHP 8 - Functions
February 28, 2013 09:46PM
77843835AlexB Wrote:
-------------------------------------------------------
> Still trying to get my head around this. The & in
> Functions is a little difficult to understand. In
> this code
>
>
Language: PHP
> <?PHP > > $Variable_Value = 10; > > print "Before the function call = " . > $Variable_Value . "<BR>"; > > example($Variable_Value); > > print "After the function call = " . > $Variable_Value; > > function example($Variable_Value) { > $Variable_Value = $Variable_Value + 10; > print "Inside of the function = " . > $Variable_Value . "<BR>"; > } > > ?> >
>
> Here you get the output of
>
> Before the function call = 10
> Inside of the function = 20
> After the function call = 10
>
> I would have thought that if you return a value
> out of the function it will also give you the
> output of:
>
> Before the function call = 10
> Inside the function call = 20
> After the function call = 20
>
> But:
>
Language: PHP
> function example(&$Variable_Value) >
>
> does not make sense. confused smiley You now change your
> original variable value. What if the original
> variable value is needed again somewhere else in
> you program is there a way to revert or would one
> just not use the & in Functions?? Would this make
> changes to variables that have been declared as
> constants??

I think the explanation they gave was nice between an original variable and a borrowed variable. Without the & it would be a borrowed copy that gets changed inside the function.. If you however want to be able to change the original you should put the & before it. That would mean you are now changing the original.

off to the next section.. enjoying this fully.. a lot of refreshing being done.. one thing I didnt actually know was that the $_SERVER variable is actually an arraythat you can explore with:

Language: PHP
<?PHP   foreach($_SERVER as $key_name => $key_value) { print $key_name . " = " . $key_value . "<br>"; }   ?>

really nice to get a little transparency for clarity smiling smiley

Student number : 7803-010-2
Email and Gtalk for support : wilcovandeijl@gmail.com
avatar Re: PHP 8 - Functions
March 04, 2013 08:50PM
Done, have a feeling I will have to revisit this section thou.

____________________________________________Nazi Coder____________________________________________

I'm not antisocial, I'm just not user friendly

"It's not a bug; it's an undocumented feature!" ~ some unknown Microsoft developer
Re: PHP 8 - Functions
March 05, 2013 04:54AM
Really interresting & learned even more in this phorum
Re: PHP 8 - Functions
March 05, 2013 11:11AM
Language: PHP
<?PHP   $Variable_Value = 10;   print "Before the function call = " . $Variable_Value . "<BR>";   example($Variable_Value);   print "After the function call = " . $Variable_Value;   function example($Variable_Value) { $Variable_Value = $Variable_Value + 10; print "Inside of the function = " . $Variable_Value . "<BR>"; }   ?>

Before I start to confuse myself here. Does this "function example(&$Variable_Value) { }" do exactly the same as a return; statement ? If that is the case why and where would you rather use this above a return; statement ?

Student Number: 78042879 (Part Time Courses)
avatar
Mac
Re: PHP 8 - Functions
March 05, 2013 12:01PM
A return statement ends the function (i.e. even if there was code after the return statement, it would not run).

Consider a shopping cart. You buy stuff, then need to pay for it. You are taken to the PayPal site to pay for it. In other words, you "leave" the shopping cart, which we call the calling program. Then you pay for your purchases on the PayPal system, and included there is a function that will end with a return statement of some sort. Think of it as a "pass back". It will pass back information that the transaction was successful or unsuccessful in a variable, which the shopping cart is waiting for before it continues with its own processes.

Of course, a return statement may pass information to another function which is waiting for it.

Here above you just want to print the result, nothing else. So you just print it.

You may want to read this http://www.larryullman.com/2009/05/24/php-variables-passing-by-value-or-reference/

In fact, everybody should read this. You will not be using it, but good to know.
avatar Re: PHP 8 - Functions
March 05, 2013 01:59PM
What mac said smile

____________________________________________Nazi Coder____________________________________________

I'm not antisocial, I'm just not user friendly

"It's not a bug; it's an undocumented feature!" ~ some unknown Microsoft developer
Re: PHP 8 - Functions
March 05, 2013 05:40PM
OK. Thank You. I think I got it.

Passing the variable by value, only sends the physical value of the the variable(that specific place in the memory storage for that variable) to a function to be processed, but leaves the original variable's value intact.

Passing the variable by reference ( example(&$Variable_Value) { } ) you are physical changing the original variable's value depending on the statements in the function.

Student Number: 78042879 (Part Time Courses)
avatar Re: PHP 8 - Functions
March 11, 2013 04:50AM
Hi there,

At first the error messages did not display for "undefined variable" when I did tut examples, so I used the

Language: PHP
Error_reporting() function

Is there somewhere else that we can change the error reporting settings, and is it best to report all errors or stick with default settings?

Cheers
avatar
Mac
Re: PHP 8 - Functions
March 11, 2013 07:48AM
Reporting all errors helps you code better... it is changes either in the php.ini file, or you can include it at the top of every page,. As said elsewhere, not all servers are set up the same, so porting your code to another server may leave you with different settings and errors you need to fix, although you can override it once again on your pages.
avatar Re: PHP 8 - Functions
March 12, 2013 02:13AM
Thanks, will change it in php.ini file for now, since I'd definitely like to code better! Done with this section, very good explanation for functions and passing variables by value vs passing values by reference.
avatar Re: PHP 8 - Functions
March 18, 2013 11:03PM
This section is quite tricky! I am done with it for now though.
Re: PHP 8 - Functions
March 19, 2013 11:08PM
There is northing hard in language php but all what i see is that we must learn with understanding. Mac, does $variable_value similar to $var_value when declaring variables
Re: PHP 8 - Functions
March 19, 2013 11:49PM
Hi 78042968

You can declare any variable with prefix "$" sign. It can be any name like $name or $value or even $sum so long as it does not start with a number like $1value or $123, but it can be $value1. Also do not use "$_" as a prefix as some functions (example: $_GET & _POST) in php uses it.

The "$variable_placeholder" is only a part that is set aside in the RAM "memory" to store a value. With PHP you can store almost any value in an variable like numbers called integers, text called strings and arrays or other values.

To the point. A variable name is a "placeholder" that you can assign any name as long as it is understandable for someone else to read and understand your code.

$sum = 1 + 1;
$name = John
$list = array()

Student Number: 78042879 (Part Time Courses)
avatar Re: PHP 8 - Functions
March 20, 2013 10:09AM
Nicely explained Anton thumbs up

____________________________________________Nazi Coder____________________________________________

I'm not antisocial, I'm just not user friendly

"It's not a bug; it's an undocumented feature!" ~ some unknown Microsoft developer
Re: PHP 8 - Functions
March 20, 2013 10:13AM
Quite interesting chapter I did, noticed functions have a somewhat similarity to stored procedures, done and completed..

..&ru..
Re: PHP 8 - Functions
March 20, 2013 10:52AM
done. I like using the include_once function to include files.
Re: PHP 8 - Functions
March 20, 2013 04:10PM
Very Cool section. Was a lot to take in and is quite tricky! Noticed how useful the "include" function was though, and will probably be using this a lot in my programming career! Now for the security section thumbs up
avatar
Mac
Re: PHP 8 - Functions
March 20, 2013 04:23PM
The two most useful things you can learn is how to effectively use include and functions. In short, it means you change code in one place and the result is effected throughout your site.

That said, as a beginner, there is nothing wrong in coding it "the long way", then sitting back and asking yourself - how can I reduce the amount of code and the amount of maintenance.

I am of the opinion that it is better to discover this (and being nudged towards it), than being taught it from the start. Daggers thrown my way..... but functions (and classes in OOP) are foreign concepts to many people, and part of the reason many people never catch on to programming or simply dislike it. For this reason I believe in writing 200 lines of code then reducing it to 50 as opposed to trying to figure out how to reduce code to 50 lines while having to deal with 200 lines of code that you have never written.

That said, in an OOP approach you sometimes write 200 lines that could have been written in 50 lines. Do not dwell on this. Let us first get the basics right.
avatar Re: PHP 8 - Functions
March 20, 2013 11:32PM
Functions are a must.
They are written once and can then be used over and over again.
I feel it's better to keep funtions in a seperate file.
The include() function is amazing.
You can basically split your entire page into different sections. Example - header, menu, content, footer.
Then using the the include() function, glue all the sections together to display the full page.
If you need to add a new menu item, just make the changes to the menu file.
You code will be neater and a lot easier to maintain.
Re: PHIP 8 - Functions
March 21, 2013 06:08PM
O, the wonderful feeling one gets after writing you own function for the first time grinning smiley.
I feel sure that somewhere out there floating around there should be a poem about this feeling.

I could however not find a poem about this feeling, I did come across this though:

http://programmers.stackexchange.com/questions/8947/i-wrote-a-poem-in-php-that-i-would-like-to-submit-to-a-literary-journal-but-wil

Favorite bit of code in this section:
Language: PHP
<?PHP foreach($_SERVER as $key_name => $key_value) { print $key_name . " = " . $key_value . "<br>"; } ?>
avatar Re: PHP 8 - Functions
March 22, 2013 11:31PM
I used to put the entire code on the page, and never used include.
I have seen it in Wordpress a lot, but never knew what it was about.
Enjoyed this chapter, and the exercises so far.smile
Re: PHP 8 - Functions
March 24, 2013 04:05PM
Hi Mac
In the example (CSD1W1S/html/functionScript.htm) given in the material, the value displays the PHP code (see below)

First Name: <INPUT TYPE = "text" name = "first" value ="<?=$first?>">

However, when it is modified as below , we get the typed in value displayed (see below)

First Name: <INPUT TYPE = "text" name = "first" value ="<?PHP echo $first; ?>">

Do we need to change anything in the code or any config file to get the first line of code to work?
avatar
Mac
Re: PHP 8 - Functions
March 25, 2013 08:02AM
Use formatted code option when posting code please.

Do not understand your question - what config file?

That is just a typo. First time this is picked up!
Re: PHP 8 - Functions
March 25, 2013 08:50AM
Language: PHP
$first = "Short Tag Example"; <?=$first?>


is the same with:

Language: PHP
<?php echo $first; ?>


But depending on the settings on your php.ini file, if :

short_open_tag = Off


then it wont work, you must change:

short_open_tag = On


in your php.ini file for php short tags to work. Hope that's what you where asking.... cool (But keep in mind that the people who will be testing your project might have short tags turned off, so I recommend you don't use them for your project. But it's a nice thing to be aware of them.)
Re: PHP 8 - Functions
March 25, 2013 09:04AM
Hi Mac
I meant php.ini.
Re: PHP 8 - Functions
March 25, 2013 09:06AM
Thanks for the tips. Cheers.
Re: PHP 8 - Functions
April 01, 2013 04:11PM
done
Re: PHP 8 - Functions
April 01, 2013 10:15PM
I am trying to create and use user-defined functions in several of my PHP scripts. I hope I won't get stuck.
Re: PHP 8 - Functions
April 06, 2013 02:40PM
Language: PHP
$to=$_POST[';to';]; $from=$_POST[';from';]; $sub=$_POST[';sub';]; $msg=$_POST[';msg';]; $header=$msg   Mail(';$to';,';$from';,';$sub';,';$msg';,';$header';); Print ("e-mail has been to" . " " .$to." "."<br>"); Print("from".$from); Print $msg;

This gives me ana error -WARNING: mail() [function]: "sendmail_from" not set in php.ini or custom "from:" header missing in C:\easyphp-5.3.3\www\test\send.php on line 11- which is mail function line
avatar
Mac
Re: PHP 8 - Functions
April 08, 2013 08:49AM
XAMPP cannot send mail, so this may be more to do with that. I have not encountered this error before....

Add

Language: PHP
sendmail_from = "from@me.com";

and see if that gets rid of the error at least.
Re: PHP 8 - Functions
April 09, 2013 10:55AM
Thanks for this!! I always wondered why i saw code like this

Language: PHP
<?=$first?>

77913523 Wrote:
-------------------------------------------------------
>
Language: PHP
> $first = "Short Tag Example"; > <?=$first?> >
>
>
> is the same with:
>
>
Language: PHP
> <?php echo $first; ?> >
>
>
> But depending on the settings on your php.ini
> file, if :
>
>
> short_open_tag = Off
>
>
>
> then it wont work, you must change:
>
>
> short_open_tag = On
>
>
>
> in your php.ini file for php short tags to work.
> Hope that's what you where asking.... cool (But
> keep in mind that the people who will be testing
> your project might have short tags turned off, so
> I recommend you don't use them for your project.
> But it's a nice thing to be aware of them.)

http://capetown-airport.co.za/
Re: PHP 8 - Functions
April 09, 2013 08:10PM
Function chapter very interesting. As with the rest of this course find the more difficult content seems to be glossed over and have fewer examples than the easier content. Have hopefully grasped the concept of how variable values change and remain the same when they go in and out of functions.
Sorry, only registered users may post in this forum.

Click here to login