728x90
반응형

* SharedPreference 정의


1. 저장


> SharedPreference 를 선언한다.


public final String PREFERENCE = "com.studio572.samplesharepreference";

SharedPreferences pref = getSharedPreferences(PREFERENCE, MODE_PRIVATE);

// SharedPreferences 의 데이터를 저장/편집 하기위해 Editor 변수를 선언한다.
SharedPreferences.Editor editor = pref.edit();

// key값에 value값을 저장한다.
// String, boolean, int, float, long 값 모두 저장가능하다.
editor.putString(key, value);

// 메모리에 있는 데이터를 저장장치에 저장한다.
editor.commit();


SharedPreferences pref = getSharedPreferences(PREFERENCE, MODE_PRIVATE);

첫번째 매개변수(PREFERENCE) : 저장/불러오기 하기 위한 key이다.

이 고유키로 앱의 할당된 저장소(data/data/[패키지 이름]/shared_prefs) 에 "com.studio572.samplesharepreference.xml" 로 저장된다.

이 때 xml 파일명은 사용자 정의가 가능하다.


> 두번째 매개변수(MODE_PRIVATE) : 프리퍼런스의 저장 모드를 정의한다.


[MODE_PRIVATE : 이 앱안에서 데이터 공유]

[MODE_WORLD_READABLE : 다른 앱과 데이터 읽기 공유]

[MODE_WORLD_WRITEABLE : 다른 앱과 데이터 쓰기 공유]




2. 불러오기


// SharedPreference 를 선언한다.
// 저장했을때와 같은 key로 xml에 접근한다.
SharedPreferences pref = getSharedPreferences(PREFERENCE, MODE_PRIVATE);

// key에 해당한 value를 불러온다.
// 두번째 매개변수는 , key에 해당하는 value값이 없을 때에는 이 값으로 대체한다.
String result = pref.getString(key, "");




아래는 SharedPreference를 사용한 예제 소스.


특별한 코드는 없으니 한번 쓰윽 훑어보시면 될 듯 합니다.


위 유튜브 동영상의 구현 소스이다.



* MainActivity.java

package com.studio572.samplesharepreference;

import android.content.Context;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

public final String PREFERENCE = "com.studio572.samplesharepreference";
public final String key01 = "key01";
public final String key02 = "key02";
public final String key03 = "key03";
public final String key04 = "key04";
public final String key05 = "key05";
private boolean isBoolean;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

final TextView result = (TextView) findViewById(R.id.result);

final EditText inputString = (EditText) findViewById(R.id.edit01);
final Button inputBooleanTrue = (Button) findViewById(R.id.edit02_1);
final Button inputBooleanFalse = (Button) findViewById(R.id.edit02_2);
final EditText inputInt = (EditText) findViewById(R.id.edit03);
final EditText inputFlaot = (EditText) findViewById(R.id.edit04);
final EditText inputLong = (EditText) findViewById(R.id.edit05);

Button saveString = (Button) findViewById(R.id.save01);
Button saveBoolean = (Button) findViewById(R.id.save02);
Button saveInt = (Button) findViewById(R.id.save03);
Button saveFloat = (Button) findViewById(R.id.save04);
Button saveLong = (Button) findViewById(R.id.save05);

Button loadString = (Button) findViewById(R.id.load01);
Button loadBoolean = (Button) findViewById(R.id.load02);
Button loadInt = (Button) findViewById(R.id.load03);
Button loadFloat = (Button) findViewById(R.id.load04);
Button loadLong = (Button) findViewById(R.id.load05);

Button clear = (Button) findViewById(R.id.clear);

inputBooleanTrue.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
isBoolean = true;
}
});
inputBooleanFalse.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
isBoolean = false;
}
});

saveString.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
setPreference(key01, inputString.getText().toString());
}
});
saveBoolean.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
setPreference(key02, isBoolean);
}
});
saveInt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
setPreference(key03, Integer.parseInt(inputInt.getText().toString()));
}
});
saveFloat.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
setPreference(key04, Float.parseFloat(inputFlaot.getText().toString()));
}
});
saveLong.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
setPreference(key05, Long.parseLong(inputLong.getText().toString()));
}
});

loadString.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
result.setText("String: " + getPreferenceString(key01));
}
});
loadBoolean.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
result.setText("Boolean: " + getPreferenceBoolean(key02));
}
});
loadInt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
result.setText("Int: " + getPreferenceInt(key03));
}
});
loadFloat.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
result.setText("Float: " + getPreferenceFloat(key04));
}
});
loadLong.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
result.setText("Long: " + getPreferenceLong(key05));
}
});

clear.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
setPreferenceClear();
}
});
}
    

// 데이터 저장 함수
public void setPreference(String key, boolean value){
SharedPreferences pref = getSharedPreferences(PREFERENCE, MODE_PRIVATE);
SharedPreferences.Editor editor = pref.edit();
editor.putBoolean(key, value);
editor.commit();
}
public void setPreference(String key, String value){
SharedPreferences pref = getSharedPreferences(PREFERENCE, MODE_PRIVATE);
SharedPreferences.Editor editor = pref.edit();
editor.putString(key, value);
editor.commit();
}
public void setPreference(String key, int value){
SharedPreferences pref = getSharedPreferences(PREFERENCE, MODE_PRIVATE);
SharedPreferences.Editor editor = pref.edit();
editor.putInt(key, value);
editor.commit();
}
public void setPreference(String key, float value){
SharedPreferences pref = getSharedPreferences(PREFERENCE, MODE_PRIVATE);
SharedPreferences.Editor editor = pref.edit();
editor.putFloat(key, value);
editor.commit();
}

public void setPreference(String key, long value){
SharedPreferences pref = getSharedPreferences(PREFERENCE, MODE_PRIVATE);
SharedPreferences.Editor editor = pref.edit();
editor.putLong(key, value);
editor.commit();
}


// 데이터 불러오기 함수
public boolean getPreferenceBoolean(String key){
SharedPreferences pref = getSharedPreferences(PREFERENCE, MODE_PRIVATE);
return pref.getBoolean(key, false);
}
public String getPreferenceString(String key){
SharedPreferences pref = getSharedPreferences(PREFERENCE, MODE_PRIVATE);
return pref.getString(key, "");
}
public int getPreferenceInt(String key){
SharedPreferences pref = getSharedPreferences(PREFERENCE, MODE_PRIVATE);
return pref.getInt(key, 0);
}
public float getPreferenceFloat(String key){
SharedPreferences pref = getSharedPreferences(PREFERENCE, MODE_PRIVATE);
return pref.getFloat(key, 0f);
}
public long getPreferenceLong(String key){
SharedPreferences pref = getSharedPreferences(PREFERENCE, MODE_PRIVATE);
return pref.getLong(key, 0l);
}

// 데이터 한개씩 삭제하는 함수
public void setPreferenceRemove(String key){
SharedPreferences pref = getSharedPreferences(PREFERENCE, MODE_PRIVATE);
SharedPreferences.Editor editor = pref.edit();
editor.remove(key);
editor.commit();
}


// 모든 데이터 삭제
public void setPreferenceClear(){
SharedPreferences pref = getSharedPreferences(PREFERENCE, MODE_PRIVATE);
SharedPreferences.Editor editor = pref.edit();
editor.clear();
editor.commit();
}

}



위 동영상에 나오는 UI대로 레이아웃을 코딩한다.

단순 레이아웃이므로 설명은 생략한다.,


* activity_main,xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="horizontal"
android:background="#88ff0000">

<TextView
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_weight="1"
android:gravity="center"
android:paddingLeft="5dp"
android:text="결과 : "
android:textColor="#000000"/>
<TextView
android:id="@+id/result"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_weight="1"
android:paddingLeft="5dp"
android:gravity="center"
android:text=""
android:textColor="#000000"
android:background="#88cccccc"/>

</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="horizontal">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:paddingLeft="5dp"
android:text="입력변수 자료형"
android:textColor="#000000"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="데이터 입력"
android:textColor="#000000"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1.2"
android:gravity="center"
android:text="저장"
android:textColor="#000000"
android:background="#cccccc"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="2dp"
android:layout_weight="1.2"
android:gravity="center"
android:text="불러오기"
android:textColor="#000000"
android:background="#cccccc"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="horizontal">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingLeft="5dp"
android:text="String"
android:textColor="#000000"/>
<EditText
android:id="@+id/edit01"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textColor="#000000"/>
<Button
android:id="@+id/save01"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="2dp"
android:layout_weight="1.2"
android:gravity="center"
android:text="저장"
android:textColor="#000000"
android:background="#cccccc"/>
<Button
android:id="@+id/load01"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="2dp"
android:layout_weight="1.2"
android:gravity="center"
android:text="불러오기"
android:textColor="#000000"
android:background="#cccccc"/>
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="horizontal">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingLeft="5dp"
android:text="Boolean"
android:textColor="#000000"/>
<Button
android:id="@+id/edit02_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1.2"
android:text="ture"
android:textColor="#000000"/>
<Button
android:id="@+id/edit02_2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1.2"
android:text="false"
android:textColor="#000000"/>
<Button
android:id="@+id/save02"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1.2"
android:gravity="center"
android:text="저장"
android:textColor="#000000"
android:background="#cccccc"/>
<Button
android:id="@+id/load02"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="2dp"
android:layout_weight="1.2"
android:gravity="center"
android:text="불러오기"
android:textColor="#000000"
android:background="#cccccc"/>
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="horizontal">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingLeft="5dp"
android:text="Int"
android:textColor="#000000"/>
<EditText
android:id="@+id/edit03"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number"
android:layout_weight="1"
android:textColor="#000000"/>
<Button
android:id="@+id/save03"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1.2"
android:text="저장"
android:textColor="#000000"
android:background="#cccccc"/>
<Button
android:id="@+id/load03"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="2dp"
android:layout_weight="1.2"
android:gravity="center"
android:text="불러오기"
android:textColor="#000000"
android:background="#cccccc"/>
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="horizontal">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingLeft="5dp"
android:text="Float"
android:textColor="#000000"/>
<EditText
android:id="@+id/edit04"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number"
android:layout_weight="1"
android:textColor="#000000"/>
<Button
android:id="@+id/save04"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1.2"
android:gravity="center"
android:text="저장"
android:textColor="#000000"
android:background="#cccccc"/>
<Button
android:id="@+id/load04"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="2dp"
android:layout_weight="1.2"
android:gravity="center"
android:text="불러오기"
android:textColor="#000000"
android:background="#cccccc"/>
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="horizontal">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingLeft="5dp"
android:text="Long"
android:textColor="#000000"/>
<EditText
android:id="@+id/edit05"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number"
android:layout_weight="1"
android:textColor="#000000"/>
<Button
android:id="@+id/save05"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1.2"
android:gravity="center"
android:text="저장"
android:textColor="#000000"
android:background="#cccccc"/>
<Button
android:id="@+id/load05"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="2dp"
android:layout_weight="1.2"
android:gravity="center"
android:text="불러오기"
android:textColor="#000000"
android:background="#cccccc"/>
</LinearLayout>
<Button
android:id="@+id/clear"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_weight="0"
android:gravity="center"
android:text="clear"
android:textColor="#000000"
android:background="#cccccc"/>

</LinearLayout>


728x90
반응형
728x90
반응형





 PHP - Include 란?





Include는 포함하다라는 의미를 갖고 있습니다

예를 들어 현재 실행시키려는 PHP 파일이 있으며, 현재 실행하고자하는 다른 PHP파일을 포함시킬때 사용하는 방법이 Include와 Require입니다


즉 PHP에서는 다른 PHP파일을 현재 PHP파일 코드안으로 불러와 사용할수 있습니다




또한 PHP에서는 include_once와 require_once라는 것이 있습니다

Include와 Require에 _once가 붙게된다면 파일을 불로올때 한번만불러오게 됩니다


예를 들어 동일한 파일을 include_once, require_once를 이용하여 2번 코드를 작성하게 되었다하여도 1번만 불러오게 됩니다


PHP에서 파일을 불러올때 4가지 형식


Include : 다른 PHP파일을 불러올때 사용

include_once : 파일을 불러올때 1번만 로드하게 됨

require :다른 PHP파일을 불러올때 사용

require_once : 파일을 불러올때 1번만 로드하게 됨






 PHP - Include와 Require 사용문법



먼저 include를 사용하는 문법을 알아보도록 하겠습니다



1
2
3
4
5
<?php
 
include '[불러올 파일명]';
 
?>


위 코드를 보시면 먼저 PHP상에서 사용하는 include라는 특별한 구문을 사용해서 불러올 파일명 안에 파일명을 입력하시면 파일을 불러오실수 있습니다




1
2
3
4
5
<?php
 
require '[불러올 파일명]';
 
?>


위 코드는 require 사용하여 파일을 불러오는 구문입니다 Include와 사용하는 방법은 동일합니다



1
2
3
4
5
<?php
 
include_once '[불러올 파일명]';
 
?>


1
2
3
4
5
<?php
 
require_once '[불러올 파일명]';
 
?>


include_once, require_once 문법은 include, require와 문법과 동일합니다







 PHP - Include 사용해보기


이제 Include를 사용해 보도록 하겠습니다




파일 : include.php


1
2
3
4
5
6
7
8
9
<?php
 
function server(){
 
  return 'server-talk';
 
}
 
?>


위 코드는 불러올 파일입니다



파일명 : get_include.php


1
2
3
4
5
6
7
<?php
 
include 'include.php';
 
echo server();
 
?>


위 코드는 include.php 파일을 불러와 불러온파일의 함수를 출력하는 코드입니다





출력내용을 확인하시면 불러온 파일의 함수의 리턴값이 출력되는것을 확인하실수 있습니다







 PHP - Include와 Require 차이점



이제 마지막으로 Include와 Require의 차이점을 알아보도록 하겠습니다

현재까지 사용한 바로는 기능상의 차이는 없었습니다


그러나 존재하지 않는 파일 등등의 경우 에러 표시가 다르게 출력됩니다



[ Include ERROR ]



위 에러내용은 Include를 잘못하였을경우에 대한 에러이며, Warning Error가 나오시는것을 확인 하실수 있습니다




[ Require ERROR ]



위 에러내용은 Require를 잘못하였을경우에 대한 에러이며, Fatal Error가 나오시는것을 확인 하실수 있습니다



Include와 Require의 에러내용으로 보았을때 Fatal 에러보다 Warning 에러보다 심각한 에러이기 때문에 Include 보다 Require가 더 엄격하게 처리한다고 볼수 있겠습니다.



728x90
반응형

'Web Programming > php' 카테고리의 다른 글

PHP 문자열 길이 (strlen, mb_strlen 함수)  (0) 2018.09.04
php data types  (0) 2018.09.04
php 배열  (0) 2018.09.03
php 함수 function  (0) 2018.09.03
php 반복문 for  (0) 2018.09.03
728x90
반응형




 PHP - 배열의 이해



기존 포스팅에서 1개의 변수에는 1개의 데이터만 담을수 있었습니다

하지만 배열을 이용하면 1개의 변수에 여러데이터를 담을 수 있습니다



위 그림을 보시면 배열을 이용하여 Arry1, Arry2, Arry3 이라는 데이터를 담았습니다 배열에 데이터를 담으면 고유번호가 있는데 이러한 고유번호를 이용하여 배열에 저장된 데이터를 접근할수 있습니다.





 PHP - 배열의 기본문법



1
2
3
<?php
$배열이름 = array(배열1, 배열2, 배열3);
?>


배열을 사용방법은 $배열이름을 정의후 array라는 예약어를 입력합니다

array 괄호안에 사용할 배열의 데이터를 ,(컴마)로 구분하여 입니다






 PHP - 배열 사용법



배열을 사용해보기 앞서 배열의 데이터를 가져오기오는데 간단한 이해가 필요합니다

배열에는 저장된 고유번호를 인덱스라 하며, 배열의 시작주소는 0번 부터시작하게 됩니다




1
2
3
4
5
6
7
<?php
$server = array('apache', 'php','mysql');
 
echo $server[0].'<br />';
echo $server[1].'<br />';
echo $server[2].'<br />';
?>


위코드를 보시면 server라는 변수에 3개의 문자열을 입력하였습니다

그리고 echo를 이용해여 인덱스로(배열의고유번호)로 출력하는 내용입니다




출력 내용일 확인하시면 배열의 내용이 출력된것을 확인하실 수 있습니다

728x90
반응형

'Web Programming > php' 카테고리의 다른 글

php data types  (0) 2018.09.04
php include, require  (0) 2018.09.03
php 함수 function  (0) 2018.09.03
php 반복문 for  (0) 2018.09.03
php login 애플리케이션  (0) 2018.08.31
728x90
반응형





 PHP - 함수 수의 실행흐름과 용도







함수의 용도


1. 함수의 데이터를 전달받아 작업을 수행하고 결과를 전달하는 구조입니다


2. 관리와 수정의 용이성이 증가합니다


3. 협업의 편리성이 증가합니다


4. 코드가 간결화하게 됩니다


5. 함수 내부의 변수가는 함수가 종료되면서  함께 소멸하게 됩니다


6. 재사용이 가능합니다



즉 필요할때마다 코드를 언제든지 실행할수 있는것이 함수목적 입니다





 PHP - 함수 기본문법



함수는 정의와 호출로 이루어져 있습니다

정의는 어떻게 동작하는가의 함수를 통해 정의를 하는것이며, 호출은 정의된 함수를 사용하게됩니다 

즉 정의를 먼저한 후에 호출를 하여 함수를 실행하게 됩니다


1
2
3
4
5
6
<?php
function 함수명( [인자], [인자]] ){
   실행코드;
   return 반환값;
}
?>


함수를 정의하려면 function이라는 키워드를 입력하고 함수의 이름이 입력합니다 그리고 괄호를 입력한다음에 괄호안에 실행코드를 작성하게 됩니다





1
2
3
4
5
6
7
8
9
<?php
function 함수명( [인자], [인자]] ){
   실행코드;
   return 반환값;
}
 
함수이름();
 
?>


함수의 호출방법은 함수이름();를 입력하게 되면 함수가 호출되게 됩니다

그다음에 함수안에 코드들이 실행하게 됩니다







 PHP - 함수 사용해보기



이번에는 직접 함수의 이름을 정의해보고 호출하여 함수안의 코드를 사용해보도록 하겠습니다


1
2
3
4
5
6
7
8
<?php
function server(){
  echo 'server-talk';
}
 
echo server();
 
?>


위 코드는 server라는 함수를 호출하면 정의한 server라는 함수를 호출하고 함수를 호출하였을 경우 함수내의 문자열을 출력하는 코드입니다







 PHP - 함수 리턴값



위에서 한 이전 코드를 보시면 함수 내에서 server 함수를 이용한 server-talk 라는 문자열을 출력했습니다 이번에는 함수를 종료하면서 돌려주는 값인 리턴 값을 이용하여 문자열을 출력해보도록 하겠습니다.



1
2
3
4
5
6
7
8
<?php
function server(){
  return 'server-talk';
}
 
echo server();
 
?>



위 코드, 출력내용을 보시면 이전과 같은 server-talk라는 문자열이 출력되는것을 확인 하실수 있습니다 이러한 이유는 function 함수내에 return이라는 부분에서 있습니다


실행흐름을 하나씩 보도록 하겠습니다


먼저 function를 정의후 server()함수를 실행하게 되면 server() 함수안에 있는 return 을 이용하여 server-talk라는 문자열을 결과값으로 돌려주게 됩니다


돌려주게되면 server()의 server-talk의 데이터로 변하게 되면 echo를 이용하여 함수에  리턴값을 줄력하게 되는것입니다







 PHP - 함수 인자(입력)값




이번에는 함수의 입력값을 전달해 보도록 하겠습니다.


1
2
3
4
5
6
7
<?php
function talk($str){
  return $str;
}
 
echo talk('server');
?>


함수의 입력값을 전달하는 방법은 함수 호출시 '()' 괄호 안에 데이터를 전달하게 됩니다


그 다음 상단의 정의한 'function talk(인자)' 함수 괄호안에 데이터가 전달되며, 전달된 데이터는 talk 함수내에서 전역적으로 사용됩니다


함수호출시 전달받은 인자값을 함수의 전달하며 인자값을 리턴하여 데이터를 출력하는 것을 보실수 있습니다.

728x90
반응형

'Web Programming > php' 카테고리의 다른 글

php include, require  (0) 2018.09.03
php 배열  (0) 2018.09.03
php 반복문 for  (0) 2018.09.03
php login 애플리케이션  (0) 2018.08.31
php 논리연산자  (0) 2018.08.31
728x90
반응형



 PHP - for 실행흐름





위 그림을 보시면 for문을 만나게되면 처음 처리하고자 초기값을 담고 for문이 실행될때마다 데이터를 변수에 저장하며, 더이상 저장할 데이터가 없을때 for문은 종료하게 됩니다






 PHP - for 기본문법



for문은 while문 보다 문법적으로 약간 복잡합니다 코드를 통해 알아보도록 하겠습니다


1
2
3
4
5
<?php
for(초기화; 반복 지속 여부; 반복 실행){
    실행코드;
}
?>



위 문법을 보시면 괄호 안에 ;(세미콜론)로 구분하여 3개의 값으로 프로그램이 반복하는 결정하는데 있습니다


초기화 : 처음 for문을 실행하였을때 1회의 한에서 처음 실행이 됩니다


반복 지속 여부 : 조건이 오게되며, 조건이 True가되면 반복문을 진행하고 False가 되면 종료하게 됩니다


반복실행 : 반복문이 실행될때마다 실행하게 됩니다







 PHP  - for 사용 해보기




이번에는 직접 for문을 사용해 보도록 하겠습니다



1
2
3
4
5
<?php
for($num = 0; $num < 4; $num++){
  echo 'server-talk'.$num."<br>";
}
?>


위 코드의 for문 괄호안에 첫번째 부분은 초기화이며, num이라는 변수의 0의 값을 대입합니다

다음에 조건이 같은지 확인한후에 for문안에 코드가 실행하고 그 다음 괄호안의 반복실행부분이 실행하게 됩니다 이러한 반복하는 과정중에 조건이 False가 되면 프로그램이 종료되게 됩니다





출력내용을 확인하시면 for문을 이용하여 반복되는 구간에 실행코드가 출력된것을 확인하실 수 있습니다

728x90
반응형

'Web Programming > php' 카테고리의 다른 글

php 배열  (0) 2018.09.03
php 함수 function  (0) 2018.09.03
php login 애플리케이션  (0) 2018.08.31
php 논리연산자  (0) 2018.08.31
php else if 문  (0) 2018.08.31

+ Recent posts