473,287 Members | 1,581 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,287 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 2525
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...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

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.