473,387 Members | 3,750 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

How does "return" work in a function?

45 32bit
I have a situation where i've used a session variable to store a user's custom session privilege to be used to determine which version of a table to show based on the user's predefined privilege. (a one character VARCHAR string which is set to 0, 1, or 2 in the SQL table where 0 means banned, 1 means regular user, and 2 means app administrator). Can I declare in a function "return priv" and use it like the following, or does "return" work differently in a function?

Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3. function privcheck() {
  4.  $priv = $_SESSION['priv'] ;
  5.  return $priv;
  6. }
  7.  
  8. if ( privcheck()=== 0) {
  9. echo 'do code for case 0';
  10. }
  11.  
  12. if ( privcheck() ===1) {
  13. echo 'do code for case 1';
  14. }
  15.  
  16. if ( privcheck() ===2) {
  17. echo 'do code for case 2';
  18. }
  19.  
  20. exit;
  21. ?>
  22.  
Mar 1 '21 #1

✓ answered by bakertaylor28

Which, from my understanding, is what the return is for in the function:
Expand|Select|Wrap|Line Numbers
  1.  function privcheck() {
  2. $priv = $_SESSION['priv'];
  3.  return $priv;
  4. }
  5.  
So That, within the If statement the following become more or less equivalent:

Expand|Select|Wrap|Line Numbers
  1. if ( $priv === foo) {
  2.  
  3. }
  4.  
Expand|Select|Wrap|Line Numbers
  1. if ( privcheck() === foo) {
  2.  
  3. }
  4.  
Also, using === instead of == prevents code injection, because of the fact that (in this case) SQL returns the value as a string, not an integer. This in turn prevents exploitative code injection by using an unexpected datatype, and then requiring that datatype.

2 2533
AjayGohil
83 64KB
Hello

firstly you have to assign value $priv and you can use == instead of === because of === check value with their datatype.

For more information you can refer following:
https://www.w3schools.com/php/keywor...from%20running.
https://www.tutorialspoint.com/php/php_functions.htm
Mar 3 '21 #2
bakertaylor28
45 32bit
Which, from my understanding, is what the return is for in the function:
Expand|Select|Wrap|Line Numbers
  1.  function privcheck() {
  2. $priv = $_SESSION['priv'];
  3.  return $priv;
  4. }
  5.  
So That, within the If statement the following become more or less equivalent:

Expand|Select|Wrap|Line Numbers
  1. if ( $priv === foo) {
  2.  
  3. }
  4.  
Expand|Select|Wrap|Line Numbers
  1. if ( privcheck() === foo) {
  2.  
  3. }
  4.  
Also, using === instead of == prevents code injection, because of the fact that (in this case) SQL returns the value as a string, not an integer. This in turn prevents exploitative code injection by using an unexpected datatype, and then requiring that datatype.
Mar 3 '21 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: wenmang | last post by:
Hi, all: I am reading Herb Sutter's article: http://www.cuj.com/documents/s=8840/cujexp0309sutter/ I have trouble to understand for following lines: class Subject { // ... public: virtual...
3
by: timmy_dale12 | last post by:
Hello , im a java programmer whos gotten tangled up in some javascripts. Im really stuck on this one can , can aybody explain this to me. I have a javscript which is to clone a table row and...
4
by: rick | last post by:
The following basic script works fine in firefox by not in IE. Can anyone spot the problem? In IE I can only delete the first line but not the lines created by javascript. Also, look at the HTML...
15
by: Generic Usenet Account | last post by:
While I have a very good feel for how inlining works, I fail to see how in the world inlining can work if the inlined function is not described "in place" in a header file, but rather defined in a...
44
by: Bamber | last post by:
Why does f get returned ? (compiled with gcc). #include<stdio.h> int func(int a); main() { int f; f=func(7); printf("f=%d",f);
7
by: Papelotte | last post by:
Hi all, I'm new to this forum and I am hoping that there is someone here who can help me. I have an ASP page that has javascript that works perfectly in IE, but not in Firefox. Can anybody tell...
48
by: Jimmy | last post by:
thanks to everyone that helped, unfortunately the code samples people gave me don't work. here is what i have so far: <% Dim oConn, oRS, randNum Randomize() randNum = (CInt(1000 * Rnd) + 1) *...
5
by: Faizmysore | last post by:
This code works good in IE, function multiuser() { document.frmOpenInteraction1.Create.disabled=true; document.frmOpenInteraction1.hidmultiuser.value="Yes"; var r =...
5
by: Pat | last post by:
Hi ASP Gurus, Please help me. Autocomplete works fine when the control does not have the focus when the page load. But when I put the focus on page load, then autocomplete does not work. I even...
1
by: sva0008 | last post by:
i have a auto suggest script that does not work in firefox , works great on IE. /******************************************************* AutoSuggest - a javascript automatic text input...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.